load_matfile

Contents

load_matfile#

class braintools.file.load_matfile(filename, header_info=True, struct_as_record=False, squeeze_me=True, verbose=True, **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) – The path to the .mat file to be loaded.

  • header_info (bool) – If True (default), excludes MATLAB header keys (‘__header__’, ‘__version__’, ‘__globals__’) from the output. If False, includes 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 (default), print loading information.

  • **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.

See also

scipy.io.loadmat

Underlying MATLAB file loader

Examples

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