Skip to main content

A package for reading MATLAB .mat files, with support for MATLAB datatypes like table and string

Project description

Mat-IO Module

The mat-io module provides tools for reading .mat files, particularly for extracting contents from user-defined objects or MATLAB datatypes such as datetime, table and string. It uses a wrapper built around scipy.io to extract raw subsystem data from MAT-files, which is then parsed and interpreted to extract object data. MAT-file versions v7 to v7.3 are supported.

mat-io can read almost all types of objects from MAT-files, including user-defined objects. Additionally, it includes utilities to convert the following MATLAB datatypes into their respective Pythonic objects:

  • string
  • datetime, duration and calendarDuration
  • table and timetable
  • containers.Map and dictionary
  • categorical
  • Enumeration Instance Arrays

Note: load_from_mat() uses a modified fork of scipy. The fork currently contains a few minor changes to scipy.io to return variable names and object metadata for all objects in a MAT-file. This change is available on Github and can be installed directly from the branch. You can also view the changes under patches/scipy_changes.patch and apply it manually. Note that you might need to rebuild as parts of the Cython code was modified. Follow the instruction on the official SciPy documentation.

Usage

Install using pip

pip install mat-io

Example

To read subsystem data from a .mat file:

from matio import load_from_mat

file_path = "path/to/your/file.mat"
data = load_from_mat(file_path, raw_data=False, add_table_attrs=False)
print(data)

Parameters

  • file_path: str Full path to the MAT-file.

  • raw_data: bool, optional

    • If False (default), returns object data as raw object data
    • If True, converts data into respective Pythonic datatypes (e.g., string, datetime and table).
  • add_table_attrs: bool, optional If True, additional properties of MATLAB table and timetable are attached to the resultant pandas.DataFrame. Works only if raw_data = False

  • mdict: dict, optional Dictionary into which MATLAB variables will be inserted. If None, a new dictionary is created and returned.

  • spmatrix: bool, optional (default = True) Whether to read MATLAB sparse matrices as SciPy sparse matrix coo_matrix.

  • **kwargs: Additional keyword arguments passed to scipy.io.loadmat. Only the following arguments are supported:

    • byte_order
    • mat_dtype
    • chars_as_strings
    • verify_compressed_data_integrity
    • variable_names

Amongst these, only variable_names is used for v7.3 MAT-files.

MATLAB objects

MATLAB objects are returned as a dictionary with the following fields:

  • _Class: The class name
  • _Props: A numpy.ndarray of dictionaries containing the property names and their contents. Dimensions are determined by the object dimensions.

If the raw_data parameter is set to False, then load_from_mat converts these objects into a corresponding Pythonic datatype. This conversion is detailed here.

Contribution

There's still lots to do! I could use your help in the following:

  • Reading function handles
  • Write object data into MAT-files
  • Write tests
  • Algorithmic optimization to integrate within the scipy.io framework
  • Documentation of the MAT-file format

Feel free to create a PR if you'd like to add something, or open up an issue if you'd like to discuss! I've also opened an issue with scipy.io detailing some of the workflow, as well as a PR to develop this iteratively. Please feel free to contribute there as well!

Acknowledgement

Big thanks to mahalex for their detailed breakdown of MAT-files. A lot of this wouldn't be possible without it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mat_io-0.3.0.tar.gz (149.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mat_io-0.3.0-py3-none-any.whl (159.4 kB view details)

Uploaded Python 3

File details

Details for the file mat_io-0.3.0.tar.gz.

File metadata

  • Download URL: mat_io-0.3.0.tar.gz
  • Upload date:
  • Size: 149.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mat_io-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f029f589d0c7a742f8ba4156c0879678fccd59009adedd96efdde6f13c29c21c
MD5 94bb038a4bf65f23fc334918923010cb
BLAKE2b-256 039d290e351fd111ed13a6094ff3d3a050a696fd7e36003dfe555d7926468299

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.3.0.tar.gz:

Publisher: python-publish.yml on foreverallama/matio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mat_io-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mat_io-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 159.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mat_io-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d4b30a84b220d9c7d9acdb7b7f3ea6672d1e02f153359a0644d5294305cb6ae
MD5 4a624acc096a9fc9e41e2c5d85498aef
BLAKE2b-256 f04079a1359d9f1b1e85592604fc445f6430a733f10fdc7f9c832dd47ae5df96

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.3.0-py3-none-any.whl:

Publisher: python-publish.yml on foreverallama/matio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page