load_matfile

Contents

load_matfile#

class braintools.file.load_matfile(filename, include_header=False, struct_as_record=False, squeeze_me=True, verbose=False, *, header_info=None, **kwargs)[source]#

A simple function to load a .mat file using scipy from Python. It uses a recursive approach for parsing properly Matlab objects.

This function recursively converts MATLAB data structures to Python types: - MATLAB structs → Python dictionaries - MATLAB cell arrays (object arrays) → Python lists - Numeric arrays → NumPy arrays

Parameters:
  • filename (str | PathLike) – The path to the .mat file to be loaded.

  • include_header (bool) – If True, includes the MATLAB header keys (‘__header__’, ‘__version__’, ‘__globals__’) in the output. If False (default), excludes them.

  • struct_as_record (bool) – Whether to load MATLAB structs as numpy record arrays, by default False. Passed to scipy.io.loadmat.

  • squeeze_me (bool) – Whether to squeeze unit matrix dimensions, by default True. Passed to scipy.io.loadmat.

  • verbose (bool) – If True, print loading information. Defaults to False.

  • header_info (bool | None) –

    Deprecated since version Use: include_header instead. Note that header_info had the inverse meaning of its name: the old header_info=True corresponds to include_header=False.

  • **kwargs – Additional keyword arguments passed to scipy.io.loadmat.

Returns:

A dictionary with the content of the .mat file, with MATLAB-specific structures converted to Python types.

Return type:

Dict[str, Any]

Raises:
  • TypeError – If filename is not a string or path-like object.

  • FileNotFoundError – If the specified file does not exist.

  • ValueError – If the path is not a file, or if loading fails.

  • NotImplementedError – If the file is a MATLAB v7.3 (HDF5) file, which scipy cannot read.

See also

save_matfile

Save a dictionary to a MATLAB .mat file.

scipy.io.loadmat

Underlying MATLAB file loader.

Examples

>>> data = load_matfile('experiment_data.mat')
>>> print(data.keys())
dict_keys(['trial_data', 'timestamps', 'spike_times'])

Include the MATLAB metadata headers:

>>> data = load_matfile('experiment_data.mat', include_header=True)