bundles / scipy latest / scipy / io / matlab / _mio / loadmat
function
scipy.io.matlab._mio:loadmat
source: /scipy/io/matlab/_mio.py :86
Signature
def loadmat ( file_name , mdict = None , appendmat = True , * , spmatrix = True , ** kwargs ) Summary
Load MATLAB file.
Parameters
file_name: strName of the mat file (do not need .mat extension if appendmat==True). Can also pass open file-like object.
mdict: dict, optionalDictionary in which to insert matfile variables.
appendmat: bool, optionalTrue to append the .mat extension to the end of the given filename, if not already present. Default is True.
spmatrix: bool, optional (default: True)If
True, return sparse matrix. Otherwise return sparse array. Format isCOOfor MatFile 4 andCSCfor MatFile 5. Only relevant for sparse variables.byte_order: str or None, optionalNone by default, implying byte order guessed from mat file. Otherwise can be one of ('native', '=', 'little', '<', 'BIG', '>').
mat_dtype: bool, optionalIf True, return arrays in same dtype as would be loaded into MATLAB (instead of the dtype with which they are saved).
squeeze_me: bool, optionalWhether to squeeze unit matrix dimensions or not.
chars_as_strings: bool, optionalWhether to convert char arrays to string arrays.
matlab_compatible: bool, optionalReturns matrices as would be loaded by MATLAB (implies squeeze_me=False, chars_as_strings=False, mat_dtype=True, struct_as_record=True).
struct_as_record: bool, optionalWhether to load MATLAB structs as NumPy record arrays, or as old-style NumPy arrays with dtype=object. Setting this flag to False replicates the behavior of scipy version 0.7.x (returning NumPy object arrays). The default setting is True, because it allows easier round-trip load and save of MATLAB files.
verify_compressed_data_integrity: bool, optionalWhether the length of compressed sequences in the MATLAB file should be checked, to ensure that they are not longer than we expect. It is advisable to enable this (the default) because overlong compressed sequences in MATLAB files generally indicate that the files have experienced some sort of corruption.
variable_names: None or sequenceIf None (the default) - read all variables in file. Otherwise, variable_names should be a sequence of strings, giving names of the MATLAB variables to read from the file. The reader will skip any variable with a name not in this sequence, possibly saving some read processing.
simplify_cells: False, optionalIf True, return a simplified dict structure (which is useful if the mat file contains cell arrays). Note that this only affects the structure of the result and not its contents (which is identical for both output structures). If True, this automatically sets struct_as_record to False and squeeze_me to True, which is required to simplify cells.
uint16_codec: str, optionalThe codec to use for decoding characters, which are stored as uint16 values. The default uses the system encoding, but this can be manually set to other values such as 'ascii', 'latin1', and 'utf-8'. This parameter is relevant only for files stored as v6 and above, and not for files stored as v4.
Returns
mat_dict: dictdictionary with variable names as keys, and loaded matrices as values.
Notes
v4 (Level 1.0), v6 and v7 to 7.2 matfiles are supported.
You will need an HDF5 Python library to read MATLAB 7.3 format mat files. Because SciPy does not supply one, we do not implement the HDF5 / 7.3 interface here.
Examples
from os.path import dirname, join as pjoin import scipy.io as sio✓
data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data') mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')✓
mat_contents = sio.loadmat(mat_fname, spmatrix=False)
✓sorted(mat_contents.keys())
✓mat_contents['testdouble']
✗matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat') matstruct_contents = sio.loadmat(matstruct_fname) teststruct = matstruct_contents['teststruct'] teststruct.dtype✓
teststruct.size teststruct.shape✓
teststruct[0, 0]['stringfield']
✗teststruct['doublefield'][0, 0]
✗matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True) matstruct_squeezed['teststruct'].shape matstruct_squeezed['teststruct']['complexfield'].shape✓
matstruct_squeezed['teststruct']['complexfield'].item()
✗Aliases
-
scipy.io.loadmat