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 load & save with .mat files, particularly perfomring read-write operations on user-defined objects or MATLAB datatypes such as datetime, table and string. It uses a wrapper built around scipy.io to read-write subsystem data in MAT-files, which is where MATLAB stores serialized object data. MAT-file versions v7 to v7.3 are supported.

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

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

Note: load_from_mat() uses a modified version of scipy. The modifications include a few minor changes to scipy.io to process mxOPAQUE_CLASS variables. You can view the changes under patches/ and apply it manually. Note that you might need to rebuild as parts of the Cython code was modified. Follow the instructions on the official SciPy documentation.

Usage

Install using pip

pip install mat-io

Available Commands

To read MATLAB objects 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,
    mdict=None,
    variable_names=None,
    **kwargs
)
print(data)

MATLAB Opaque objects are returned as an instance of class MatioOpaque with the following attributes:

  • classname: The class name, including namespace qualifiers (if any).
  • type_system: An interal MATLAB type identifier. Usually MCOS, but could also be java or handle.
  • properties: A dictionary containing the property names and property values.

These objects are contained within numpy.ndarray in case of object arrays. If the raw_data parameter is set to False, then load_from_mat converts these objects into a corresponding Pythonic datatype.


To write Python objects to a .mat file:

from matio import save_to_mat

file_path = "path/to/your/file.mat"
mdict = {"var1": data1, "var2": data2}
save_to_mat(
    file_path,
    mdict=mdict,
    version="v7",
    do_compression=True,
    global_vars=None,
    oned_as="row",
)

When writing objects, matio tries to guess the class name of the object based on its datatype. For example, pandas.DataFrames could be read in as table or timetable. You can also explicitly mention the object by wrapping your data around an instance of MatioOpaque as follows:

from matio import MatioOpaque, save_to_mat

df = some_dataframe
mat_df = MatioOpaque(properties=df, classname="table")
mdict = {"table1": mat_df}
data = save_to_mat(file_path="temp.mat", mdict=mdict)

For user-defined classes, a dictionary of property name-value pairs must be wrapped around a MatioOpaque instance. In case of arrays, these objects should be contained within a numpy.ndarray with dtype=object and each MatioOpaque instance should be flagged with the is_array attribute. All user-defined classes default to the MCOS type system.

Notes

  • Extra keyword arguments (**kwargs) are passed directly to scipy.io.loadmat or scipy.io.savemat.

  • For conversion rules between MATLAB and Python datatypes, see the documentation.

  • To differentiate between MATLAB character and string arrays, it would be ideal to wrap a numpy string array around a MatioOpaque instance. However, this is not feasible all the time, especially as MATLAB seems to be using strings in most of their new datatypes. Hence, this module uses Scipy's convention of converting Python str, list(str) or numpy.array(dtype=<U1/S1) to a character array. Numpy string arrays with multiple strings is converted to a MATLAB string

Contribution

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.4.2.tar.gz (158.3 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.4.2-py3-none-any.whl (170.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mat_io-0.4.2.tar.gz
  • Upload date:
  • Size: 158.3 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.4.2.tar.gz
Algorithm Hash digest
SHA256 68decf70ba96f1fe88ebad1175125b815e9dc8a81d36b781e59fd58fd1145d52
MD5 3bd04971dd7b00d596d3978bb5ac5f40
BLAKE2b-256 c453329ebced41d6fa04bf6a9f5a65a11dec37528a05807bd080fa319c1ea48f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.4.2.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.4.2-py3-none-any.whl.

File metadata

  • Download URL: mat_io-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 170.3 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.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ef56213e0611f7317d79de6aae9059954921ac29f590e375124d53ef76371862
MD5 ffcc4a962234f715bb69bc52511040c5
BLAKE2b-256 5ea1edac3b26b69115e42f85eabe4e84af5b6d0f049a9ee83bc85d5916137476

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.4.2-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