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 loading and saving MAT-files, including MATLAB's classdef-based datatypes such as datetime, table and string. It supports almost all MATLAB object types, 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

MAT-file versions v6, v7 and v7.3 are supported.

  • Versions v6 and v7 uses a modified version of scipy.io under the hood
  • Version v7.3 uses h5py to write in the HDF5 format.

Data is returned in the same format as scipy.io.loadmat does.

Installation

pip install mat-io

Usage

Loading MAT-files

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,
)
  • raw_data: If True, returns raw property maps of Opaque class objects.
  • add_table_attrs: If True, adds custom Matlab Table or Timetable properties as pandas.DataFrame attributes.
  • mdict: If provided, this dictionary will be updated with the data from a MAT-file.
  • variable_names: A list of variable names to load from file.

Saving MAT-files

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.3",
    global_vars=None,
    oned_as="col",
    do_compression=True,
)
  • file_path: The file to save data.
  • mdict: A dictionary of {var_name: var_data} to write to MAT-files.
  • version: The MAT-file version to save to. Supported versions are v7.3 and v7. Defaults to v7.3 which is based on the HDF5 format.
  • global_vars: A list of variable names that are to be marked as global variables.
  • saveobj_classes: A list of class names that implement saveobj methods.
  • oned_as: The 2D shape to apply to unit numpy.ndarrays. Either row or col. Defaults to col.
  • do_compression: If False, does not compress data when saving.

List variables in a MAT-file

from matio import whosmat

file_path = "path/to/your/file.mat"
vars = whosmat(file_path)
# Returns (variable_name, dims, datatype/classname)
print(vars)

Opaque Class Objects

Opaque class objects are what MATLAB calls object instances. Opaque objects have different types. The most common is MCOS, which is used for all user-defined classdefs, enumeration classes, as well as most MATLAB datatypes like string, datetime and table.

Opaque objects are returned as an instance of class MatlabOpaque 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.

If the raw_data parameter is set to False, then load_from_mat converts these objects into a corresponding Pythonic datatype, if available. For a list of conversion rules between MATLAB and Python datatypes, see the documentation.

When writing objects, matio tries to guess the class name of the object. For example, pandas.DataFrames could be read in as table or timetable. User-defined objects must contain a dictionary of property name, value pairs wrapped around a MatlabOpaque instance.

from matio import save_to_mat
from matio.utils import MatlabOpaque

prop_map = {"prop1": val1, "prop2": val2}
mat_obj = MatlabOpaque(properties=prop_map, classname="MyClass")
mdict = {"var1": mat_obj}
data = save_to_mat(file_path="temp.mat", mdict=mdict)

If the class implements a custom saveobj method, this can be specified in the saveobj_classes argument in save_to_mat. The property map for such classes must contain a single property called any. Typically, the value of this property would be in a format output by the saveobj method. This would be the input for the loadobj method in the class, if defined. More information here.

Wrapper Classes

This package uses wrapper classes to represent Matlab object data to help distinguish from basic datatypes. These are as follows:

  • MatlabOpaque: A wrapper class for all opaque objects with three attributes: properties, classname, type_system. properties is a name-value pair dictionary for each property of the class saved to a MAT-file
  • MatlabOpaqueArray: A wrapper class subclassed from numpy.ndarray to represent object arrays. Each item in this array is a MatlabOpaque object.
  • MatlabEnumerationArray: A wrapper class subclassed from numpy.ndarray to represent enumeration instance arrays. Each item in this array is of type enum.Enum.
  • MatlabContainerMap: A wrapper class subclassed from collections.UserDict to represent container.Map objects. During save, dictionaries are converted to a struct. Wrap dictionaries around MatlabContainerMap to write to container.Map instead.

To save these types to a MAT-file, data must be wrapped around the relevant wrapper class. These can be imported from matio.utils. An example is shown below:

# Save dictionary as container.Map
from matio.utils import MatlabContainerMap
from matio import save_to_mat

map = {"a": 1, "b": 2}
map = MatlabContainerMap(map)
var_dict = {"myVar": map}
save_to_mat("file.mat", var_dict)

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!

Acknowledgement

Huge thanks to mahalex for their 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.5.0.tar.gz (472.7 kB view details)

Uploaded Source

Built Distributions

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

mat_io-0.5.0-cp313-cp313-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

mat_io-0.5.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

mat_io-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

mat_io-0.5.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

mat_io-0.5.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

mat_io-0.5.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for mat_io-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e8ee4c0001a62c097281d403db1aafec7181918823ef31101f06eeca75c62cf3
MD5 fe0ba96efe15b0561aa0fc7af66d0daa
BLAKE2b-256 0cdf3c35971177b3b91a279f3ff3b7c7502e444f4961d484b03539bc64d36f5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.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.5.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c0647ffe2ab86dd7d4a65beffbe365e4542becdbf17d848f9a4d4ee04c591020
MD5 67efad858b5dee40a031e917574ceaeb
BLAKE2b-256 df7a445ac5f60b086c25aeae1c670e871bcbbcdbd95631e32ed796f005daf1bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp313-cp313-musllinux_1_2_x86_64.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.

File details

Details for the file mat_io-0.5.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 56db133815aebdfd7f6d2233e222181099b9833b89a84b523d2b0a6269313fe9
MD5 f8c52a6927655cdc280fb12a3a8fe33f
BLAKE2b-256 5a95667958bff347ca7ed19866376fde7a9dbb4c79dd77587e6a042ebdc0127c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.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.

File details

Details for the file mat_io-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b7da2f7a2ef891d9dcb3a82946aaba45fec07b7e49c7c64785caf1a91b0fa618
MD5 a6638da83bac5b604b6d7b6c356d6f19
BLAKE2b-256 9a4260b7b93dd8b06665c6dd094c000ffc32810a9690e4cdc063bcdf037df713

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp312-cp312-musllinux_1_2_x86_64.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.

File details

Details for the file mat_io-0.5.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 98c42fbc6d5b1e5bb63d006e5ed9485cc74da1432d36ea4704eac39551d24bc5
MD5 c643fc12486427db139e46b482bd1543
BLAKE2b-256 6102e12b73acacf244127185801387ffdf5e645e89ee0e247a0bfcb360d322fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.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.

File details

Details for the file mat_io-0.5.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1fc672a3693ec945a5405b8acaf63a9d11dfe9e28c53749b8d299ac88ac72c3a
MD5 646a504586455d288a414b9b2c96be13
BLAKE2b-256 c78e9e2b521c29a5c9cda55c54cf320f4e5a6632641a6a87ed69ac6300f96c6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp311-cp311-musllinux_1_2_x86_64.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.

File details

Details for the file mat_io-0.5.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.5.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 93f4462fa826966cc08eba6db52053d715fcc1b46e661ea0cd2729b637790987
MD5 a6edea47fd5309d515c34db265fc591a
BLAKE2b-256 d22231ceedcbe9a6924ef8d73c266d7507fa3568fffb713201ed5cef8b80307a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.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