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.6.1.tar.gz (485.1 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.6.1-cp313-cp313-win_amd64.whl (437.5 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-0.6.1-cp313-cp313-win32.whl (423.1 kB view details)

Uploaded CPython 3.13Windows x86

mat_io-0.6.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.6.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.6.1-cp313-cp313-macosx_11_0_arm64.whl (451.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-0.6.1-cp312-cp312-win_amd64.whl (438.5 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-0.6.1-cp312-cp312-win32.whl (423.5 kB view details)

Uploaded CPython 3.12Windows x86

mat_io-0.6.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.6.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.6.1-cp312-cp312-macosx_11_0_arm64.whl (452.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-0.6.1-cp311-cp311-win_amd64.whl (440.3 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-0.6.1-cp311-cp311-win32.whl (426.1 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-0.6.1-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.6.1-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.6.1-cp311-cp311-macosx_11_0_arm64.whl (454.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-0.6.1.tar.gz
  • Upload date:
  • Size: 485.1 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.6.1.tar.gz
Algorithm Hash digest
SHA256 992a6a0aca1e3c7d0f52d0bc261b9da4ae705927884cf7aebb513c96894de011
MD5 71806db3570dc98c55e10b5dc549f126
BLAKE2b-256 87c597b072141d3f907d68eabcd7e9307aab0c0fbb339e78438baadf8b309add

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 437.5 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.6.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fe5d94a34d45a463c685a80698f29099b44451d397e39759b764560ea494165f
MD5 767d3f39f8439638627efc3a077961b8
BLAKE2b-256 8010be696fb32255411154d56e52f973aeffa2f7e18aba29c782ced48266b946

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp313-cp313-win32.whl
  • Upload date:
  • Size: 423.1 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.6.1-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 188ec8031ccd6502ef058572f119f5a21c682babd9ef37aed6424c889d1caad6
MD5 2690eda6b6b8c9828e04df2566168445
BLAKE2b-256 e1a8ca71683a2dc3618cecab5131a2056c2e577c7810ff137b59c76f52e232b8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 21e65f8a999db81fda54456b962f5e00eaef93fa2627039147e595c5807b172e
MD5 70ab5ed7ee3349cef15d5312bbf1c417
BLAKE2b-256 5331bb3f8f7de7c488cc520a16c15d6bbe3a13ff771ac3739cb9873582e344d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 273c37acada682c1afc4865d96e644c18d96f1b0d8c8e638b4dcc8db11f75228
MD5 62a80b9ba360aa7c89b7508aa231c479
BLAKE2b-256 360418d99284fc7198d88fe383f302d27f9e85dff8be96f7ff5f9d5d511c2a48

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0f9199c0f6c6977f78c840598d3bb0d0661e3c968d460233e29a6a04edc6a088
MD5 6908d54dc6582254426428353cf735f2
BLAKE2b-256 78d680b75329e5307a7f1a5fc7dba62a17bd07c15b71b11188a5d24ce2105d53

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 438.5 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.6.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7df3674c1c648b840276963ee023d8db1642cb4d207b3c3afc15d099a0d3e206
MD5 689550290cf4cbbdf13a313fa6dfcaff
BLAKE2b-256 ffd1257e6d0126a803f53a2bee8c8aa4e81211e1c97b78ca22b4c3c92352dc5d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 423.5 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.6.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 306610ca794b5caf183097379cfd1d39cc5fad92ef25b8df459ed761470edcf9
MD5 bccf8dc5a28e321143eae5adbf2776c1
BLAKE2b-256 f775a4f153264de4be98f6473410dbfa9824aff9d0b60f8a438c475bf70abbfc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ca1a32486324ecf880455bcb1021b671fee7a6842067164a750bb3f4bbcd3886
MD5 a72581a62ca73933a512f8c7d2866f4d
BLAKE2b-256 37918a4057bbf53d48ec9bc5a10f853354b65002dca732db85840cabb1fd3e53

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5e4f19ad054c1c395fe4fcfb0d6da1308928bb60a08a95aa133b71015eac19d4
MD5 95c4b2d397cb672e08eb1c9aa21e16e7
BLAKE2b-256 27b8fa8750f8de4ebe649e5d59d3b654a24ba020df5c18b3f11d5e4a141b2133

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9671a43296199ac9946d0223b76ce6102653778aec7908b51bf7c116cb103dbc
MD5 3e69b0e9b795c648e08cb6ad7bdedcad
BLAKE2b-256 0907ccfe9694c3c708ea2054f36a6787c315e6302e0cf041a546d7b994f5f2cd

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 440.3 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.6.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 84de085a7217f081a29ce7b26df459e0b86b886b396ce7383113bca7d8a3bad6
MD5 0c83656dcfd46c1b79013a559a94d796
BLAKE2b-256 de90615e3333121eb88331118cdd83db8df0cd739de2a1b2532aeb1b4c4ff5ac

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 426.1 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.6.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 cc36488f74561f02b7651f80faf6171d21bbe7e0cbeeaa42c33ccdff3f247fbc
MD5 715f17618dd50e43d8afe4d606b6ce8b
BLAKE2b-256 f397249ab3969494c4d65a37b16aa7b4d120f3169667be25fe358f5088e4919b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6dbd2b73065cfc74230b3669fc0a3d4ac7c1f1f1b486428a150138525f6ad76e
MD5 b6f3cf0d497e3946b97e28fed8992247
BLAKE2b-256 d730d6645456929d4ab4503cf75ed3f98faf931fda2a4c28a4ce687757d328c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a3452811f65cb0b76549250e18089b956ae3a62eb2cb02e7873447e30c8c9d5c
MD5 2da2bf0248d1346086cb5243be83c288
BLAKE2b-256 0af17a253405f9fdf6d4ebe2bb109474669119e10e7547d15c6b15a5b5ea8e6c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 668c1bf620cf163ebddd725b3a041f56318be9d618996cb9604a5a4514679df9
MD5 f4f8d6f568674a85a2408b72cdc03ec5
BLAKE2b-256 898b9bced2dcfdac618fd0fe0aad57a478d0071e7d4fd12d83637ca06dc44a89

See more details on using hashes here.

Provenance

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