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.
  • class_alias: This is an optional attribute containing class aliases, if any.

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.7.0.tar.gz (588.0 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.7.0-cp313-cp313-win_amd64.whl (438.8 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-0.7.0-cp313-cp313-win32.whl (424.8 kB view details)

Uploaded CPython 3.13Windows x86

mat_io-0.7.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.7.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.7.0-cp313-cp313-macosx_11_0_arm64.whl (452.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-0.7.0-cp312-cp312-win_amd64.whl (439.7 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-0.7.0-cp312-cp312-win32.whl (425.1 kB view details)

Uploaded CPython 3.12Windows x86

mat_io-0.7.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.7.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.7.0-cp312-cp312-macosx_11_0_arm64.whl (453.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-0.7.0-cp311-cp311-win_amd64.whl (441.6 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-0.7.0-cp311-cp311-win32.whl (427.5 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-0.7.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

mat_io-0.7.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.1 MB view details)

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

mat_io-0.7.0-cp311-cp311-macosx_11_0_arm64.whl (455.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-0.7.0.tar.gz
  • Upload date:
  • Size: 588.0 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.7.0.tar.gz
Algorithm Hash digest
SHA256 bb15aae34cad66ce6980450f5d215748f7dc042dc7eefec1825448db1bfb8620
MD5 f98e5a405acd0aeae4818bcc7d8e7804
BLAKE2b-256 958415fc91704bec35e82388e3c61a2db3c4bb518873cc9f7542de0c4a48fd33

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 438.8 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dabb484b236bf47875d91cdc76e0ddfa6dc534bdaa9a9711e8246cf20b05ae5d
MD5 ed83fdd22b9f52c2dfb48416fca62de7
BLAKE2b-256 5ef6e68570658492494b23c54e3f8a85d08165f94e086f3ae20327d9e0df357d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp313-cp313-win_amd64.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.7.0-cp313-cp313-win32.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp313-cp313-win32.whl
  • Upload date:
  • Size: 424.8 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 4139dc938b16ab3b6afa68f0d6f1c9b722dc9f09570766849a75a021f0c772f6
MD5 db95513e7990df688f3bdfddbd4342d4
BLAKE2b-256 544a3c45f5a5456ae115466acbf0bbd927e89606045472f34f4f25b8eda6eb57

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.7.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4276de3b2b1efb7313380fead7af9fd60a12ed01ae2bb606489a106beba7f276
MD5 b5372c41ba9dbba07fe45761019dd515
BLAKE2b-256 746cda10a376e3b11b7064275b652efc222ff8ffee8ac6d51e3e38ce854e5cab

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.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.7.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1efe0868feee6e0ba4576f3adcd4b308a1702e4e9d127d5830dcf13da725c749
MD5 0b004d743bb7a2186020f5f988ad3efc
BLAKE2b-256 076060cb6abe62eab2541784e7832eb126a8c238c7e02756eed1bd28a6188167

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mat_io-0.7.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8c4ddd40b08804ba845dcbd27f781c80d08c4a447aa5031dab13ddc54ac697b
MD5 01fdbf05001080bef15b5ad3264bcdaa
BLAKE2b-256 1180d6a7a3af329f0c260fffa8a9470bf750168394fd1890f2fe6ef88dc5757f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp313-cp313-macosx_11_0_arm64.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.7.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 439.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 658455e03e75697bd6b103077937c6fa2a458f484de6e5571c401e9ee8b3940a
MD5 7813e4b10c6e20e9509b667e854920b6
BLAKE2b-256 4ea599cf4e6adbdd89f5738df8d0aae6b78fc359e153116df77b323b6ff3e67e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp312-cp312-win_amd64.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.7.0-cp312-cp312-win32.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp312-cp312-win32.whl
  • Upload date:
  • Size: 425.1 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 3d01d713e84bd492b665c29fc9e73487b123554ab14f153bc01fbe424466b07a
MD5 ef611dcc98145e2234727b27b01fe728
BLAKE2b-256 33aa9db92232bd8a47be6790825b41e92e613d9e2de209446cf39f5975ee5140

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp312-cp312-win32.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.7.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.7.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e8c848469d9618bacb2b318b00cf4e4ddb779d714dc818d29c3cd96eb17af719
MD5 d6ca1e1e5c0a059875f2b350d9e3d743
BLAKE2b-256 4262cd18d6ff1868d11607763c311cf81e40567e44399222b516d85aad96d756

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.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.7.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d23cf988b2ca9de7606de77e3e0ddb155ded2f6635219b436124c94c869ec7a9
MD5 4a33f28e86a6bea414ae9d983dfb8ad2
BLAKE2b-256 2ff25deefb473072524640a88eb81cb21500e43dfd71d8bef71777c294d230b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mat_io-0.7.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a7f576a43151a5179dd7ded7d955a91112ec06a25f5d5c2def84bd5e6ccf687
MD5 535662639ddf3b7b997d6834ed662be5
BLAKE2b-256 9dd5f8db6d0f5843c628f31a733e6770a7ffc6e3d14e6df3e71b92e5461ade30

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp312-cp312-macosx_11_0_arm64.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.7.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 441.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ec31f7d7ee67120a59bec4d838887cdf320b4d9741a4aa512e0af8fc33af28df
MD5 7b65e7e2fce1d76c285eb652cd4879c1
BLAKE2b-256 f096e2571edf64dbc3d5018b299a3bb8dab35d3d08f32553cb63a0d481b52fb8

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp311-cp311-win_amd64.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.7.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: mat_io-0.7.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 427.5 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mat_io-0.7.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 5bc1609cba85d04d30d9b29e43ced5725c8ac1b53ae240c66f66d00d0f05bc3a
MD5 6a69322d26015ddf5ab65d7fa88187f4
BLAKE2b-256 9cd6587736861e5886a2a2efa9768a1cee7a1e1e42992b8cfa3c7e6748ef51bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.0-cp311-cp311-win32.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.7.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mat_io-0.7.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ad14c98772c0d85349a2b8a98fcc2d92738d3272dcd0822756aecceacd9d3a66
MD5 bf21289c821510e49966c50ec423ab57
BLAKE2b-256 6b5155c0d1949a8dbd3e229614c444441fc1a4095a3bacada3d1947272e27e08

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.7.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.7.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.7.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 08ab31ffc901a18b9f4719f65d41126fdd092254c0e7d753ec264c94d6188cc4
MD5 566035299b6d20c273f345e410967d63
BLAKE2b-256 e43e62285ed7642d1afef5f9cfdd3b096737fb3f8fefd7de8df1b1f5372b58d3

See more details on using hashes here.

Provenance

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

File details

Details for the file mat_io-0.7.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mat_io-0.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3aa333257173e0f56267597f7322c995005e8357625cde0cb13ed92932d29d15
MD5 4f85bcf1b7031d7dd8c389fea4aedbec
BLAKE2b-256 4b5a21fd9e851fedf163b1eaf00c8895edc2eb7a52ba3636fc60be749ae6e466

See more details on using hashes here.

Provenance

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