bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / io / matlab / _mio / loadmat
function
scipy.io.matlab._mio:loadmat
source: build-install/usr/lib/python3.14/site-packages/scipy/io/matlab/_mio.py:82
Signature
def loadmat(file_name, mdict = None, appendmat = True, *, spmatrix = <object object at 0x0000>, **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.**kwargsThe following additional keyword arguments can be passed:
byte_order
None by default, implying byte order guessed from mat file. Otherwise can be one of ('native', '=', 'little', '<', 'BIG', '>').
mat_dtype
If True, return arrays in same dtype as would be loaded into MATLAB (instead of the dtype with which they are saved).
squeeze_me
Whether to squeeze unit matrix dimensions or not.
chars_as_strings
Whether to convert char arrays to string arrays.
matlab_compatible
Returns 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
Whether 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
Whether 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
If None (the default) - read all variables in file. Otherwise,
variable_namesshould 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
If 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_recordto False andsqueeze_meto True, which is required to simplify cells.uint16_codec
The 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())
✓['__globals__', '__header__', '__version__', 'testdouble']
mat_contents['testdouble']
✗array([[0. , 0.79, 1.57, 2.36, 3.14, 3.93, 4.71, 5.5 , 6.28]])By default SciPy reads MATLAB structs as structured NumPy arrays where the dtype fields are of type `object` and the names correspond to the MATLAB struct field names. This can be disabled by setting the optional argument `struct_as_record=False`. Get the filename for an example .mat file that contains a MATLAB struct called `teststruct` and load the contents.
matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat') matstruct_contents = sio.loadmat(matstruct_fname) teststruct = matstruct_contents['teststruct'] teststruct.dtype✓
dtype([('stringfield', 'O'), ('doublefield', 'O'), ('complexfield', 'O')])
The size of the structured array is the size of the MATLAB struct, not the
number of elements in any particular field. The shape defaults to 2-D
unless the optional argument `squeeze_me=True`, in which case all length 1
dimensions are removed.
teststruct.size teststruct.shape✓
(1, 1)Get the 'stringfield' of the first element in the MATLAB struct.
teststruct[0, 0]['stringfield']
✗array(['Rats live on no evil star.'], dtype='<U26')Get the first element of the 'doublefield'.
teststruct['doublefield'][0, 0]
✗array([[1.41, 2.72, 3.14]])Load the MATLAB struct, squeezing out length 1 dimensions, and get the item from the 'complexfield'.
matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True) matstruct_squeezed['teststruct'].shape matstruct_squeezed['teststruct']['complexfield'].shape✓
()
matstruct_squeezed['teststruct']['complexfield'].item()
✗array([1.41+1.41j, 2.72+2.72j, 3.14+3.14j])
Aliases
scipy.io.loadmat