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.1.tar.gz (472.8 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.1-cp313-cp313-win_amd64.whl (427.8 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-0.5.1-cp313-cp313-win32.whl (413.5 kB view details)

Uploaded CPython 3.13Windows x86

mat_io-0.5.1-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.1-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.1-cp313-cp313-macosx_11_0_arm64.whl (446.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-0.5.1-cp312-cp312-win_amd64.whl (428.7 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-0.5.1-cp312-cp312-win32.whl (414.2 kB view details)

Uploaded CPython 3.12Windows x86

mat_io-0.5.1-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.1-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.1-cp312-cp312-macosx_11_0_arm64.whl (449.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-0.5.1-cp311-cp311-win_amd64.whl (430.5 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-0.5.1-cp311-cp311-win32.whl (416.7 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-0.5.1-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.1-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

mat_io-0.5.1-cp311-cp311-macosx_11_0_arm64.whl (450.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-0.5.1.tar.gz
  • Upload date:
  • Size: 472.8 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.1.tar.gz
Algorithm Hash digest
SHA256 98ab92092d705b41ca2e16f23488c0f38022c2ba38347959840a9332f48d09bc
MD5 82aa06ad89bab776207c6f5ba677df72
BLAKE2b-256 4e35fd0a245fc4c1956c55682692aea4d55f68563e2b118fab0c155e42270610

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 427.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.5.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4c2ba9d9e2461ceb5cc93cd741ac78757374c5ab6b3f936636a10e1aecddca0c
MD5 8771dff56a5be096c36bd681ff3f3e2f
BLAKE2b-256 05c86627f0ab7df08bbe2b416a5b233b3cca79aec802690d63e3b4c48df668f0

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp313-cp313-win32.whl
  • Upload date:
  • Size: 413.5 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.5.1-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 3199a86d96754078f54d14830f976c62a19e4b3ac0fee7fb867792fcd9af560f
MD5 253202edaa0fce761304290b191927ec
BLAKE2b-256 f5b233151e8379bd02883e5217b83b29ebe7ee20c822f99aeb1c41cf36b3f288

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4c09ffbe76ec0ad6e53773d5e85cbec470a63560547e79b70b7f4db7df57567c
MD5 6b0b35b8b7358ef0988f6600f4c43dbf
BLAKE2b-256 3d955f2ca00c1058e6d14dea4803ad29d26d8a66fe60913e18d6ce3877db7603

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.1-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.1-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.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f762e0dbbfe9a7c11993c1c5ecfcabb64c8c8d7b2446279cd6d99ce4f86a8a4
MD5 d48a3fcadfcef1540ead3b066b711fad
BLAKE2b-256 c682b84f297247832f6de5545252351fa1cd139eb8aa684d5fdad36ab8966b4a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e7a6eced45c90f0a52f456ca3701bc71100279a0e86573972a58e83828c93101
MD5 1ee56088cf1deb230afcf3156c357401
BLAKE2b-256 d3bb092b7af73e1c04fe3c3e2343a1f122667545f30ed3b224846311a9912e75

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 428.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.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f27324610042daf6dd796d5fcdfb895f8dff65a181852816339d0f6d649da3c5
MD5 403e5bf0d24c375df03299705ec5c36f
BLAKE2b-256 b0ad6c5a68c147a54bed4eb81a23dc53dc3dca27f16b4714fed82fdf615084b6

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 414.2 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.5.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 6f8c842f5d84bd575822b104115a39df22142044799d977551a890d2014bcaea
MD5 771ac4a308e2652c6ffdbe34b3ee515a
BLAKE2b-256 91432f576b836eeca4670f7e779c87f89361a01babf86feb90ca554b0b841068

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 93e601bb34dd9aff89c8506daae31412589567bd254119d3ecc034dc6e474c60
MD5 95e16eccc979e3d099620e33b73a0402
BLAKE2b-256 f016fc4b2dadf56a48e66c1a8185eb7ccc29c158b02f9e35a9d4a3092dcd51f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.1-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.1-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.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 59ace5e1b20cd0573a3b3d826ee533798eddcf0440dc83a8e63ffa82683e8391
MD5 95b2d0fdac88e5b1220d17b48ccf00a7
BLAKE2b-256 b187b21c32f5091a408b52b35bd0f49d2cde5a4c387483c267a6915432c096c0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e441a3d37547388ea36ec8d054958bce9d36e809ffcb5ec959910280091a399
MD5 03c5cf5ea2bd8aa1d8d007b7dcbb015d
BLAKE2b-256 6a2fa14f2004cfcd2bbd4f912f43fa7e786706e1d32746c487690964196422cd

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 430.5 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.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7ba037c6329ab8b5bacce477da139010f5dad83fca07ac055aba82c3b72a73f3
MD5 5c6a26e2eddce9e1aca8bf15ee19331a
BLAKE2b-256 971b032bccb592a662725feffbb55e03bfee2fb08deb109fff0dcdc1212a69d3

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.5.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 416.7 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.5.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 22bfbaf5bea30d3a45316bda92047e65b198e60e51d05e6e878e10f112aaddbb
MD5 4fd13d8c9992894f6e247458cd8d896e
BLAKE2b-256 18fe831030cda7fda18aa1249014d2f704d21e3de06d23599553294f7d9b098d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0d9412f10489e1bcd6a8e0dfcef91938c80259e38ce51ef117294e2ed11959fb
MD5 cae0f9a9bbc024f3bff6843b42aa7d58
BLAKE2b-256 111f0a00562347ceb91f1ed5cadfa2ca1d2d04ef04a6e110b1aa57bc8b96f74c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.5.1-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.1-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.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 330fcf0aec5267c4e93beacee14bc859472b57dd8b5790c8330fd6a14d6c3f69
MD5 e6078b12af653beeff77760a7eb2d12b
BLAKE2b-256 7a3272c4df681a22f94daa721e1880c10a4c7438c6e99de42276b0514b0b609d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.5.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 962ffb9a10eb1ad0cde84a4374fc13336027919b57e55dec82e514b08582e20a
MD5 8b6fd724c2eda69620396a88a4fb111d
BLAKE2b-256 a8c3dd380dbac1d4e73b443d6dfb42fff47b2152afcac9125edfd57bb58c99d1

See more details on using hashes here.

Provenance

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