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.6.0.tar.gz (485.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.6.0-cp313-cp313-win_amd64.whl (437.4 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-0.6.0-cp313-cp313-win32.whl (423.0 kB view details)

Uploaded CPython 3.13Windows x86

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

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-0.6.0-cp312-cp312-win_amd64.whl (438.3 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-0.6.0-cp312-cp312-win32.whl (423.4 kB view details)

Uploaded CPython 3.12Windows x86

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

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-0.6.0-cp311-cp311-win_amd64.whl (440.1 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-0.6.0-cp311-cp311-win32.whl (425.9 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-0.6.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.6.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.6.0-cp311-cp311-macosx_11_0_arm64.whl (453.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-0.6.0.tar.gz
  • Upload date:
  • Size: 485.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.6.0.tar.gz
Algorithm Hash digest
SHA256 936184897bbd05b3de4bfdc00e325c239c767af547c1faf51bbbe4a004b51502
MD5 0ea3c497d2f33bbc7c5898a9ed77f5dd
BLAKE2b-256 64ee6f5a754c950e68276a153085c8e12aafaab57204c090c5c2f3f9d02eb09d

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 437.4 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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 73a55e768d6a9478ef572acf837e758c7196b7a7dd4351c4ee2ee4cccd3563c3
MD5 836496d0a5d3c819c81b478e443d8e7e
BLAKE2b-256 038603fd951601cc5693fdaddb217b5ada21b87f82f3e03b3b47b816c1402d1a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp313-cp313-win32.whl
  • Upload date:
  • Size: 423.0 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.0-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 3c3c4d2ded6bee3fedaf3f94a9d285eeb16870af0695498e66daf37225b06bd2
MD5 4f84af5df255063b6b0e8fd342153fe5
BLAKE2b-256 453e6e9e081ac61456d99dc774d3a2c11707e4e2fa3cb15496dc315453bc70ff

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9f8108f2dbe8bcad0477c4c2adbdd4599a44108b3cee0307429dd21db34a88a0
MD5 3ed94958656b465c3fabf28de514ccab
BLAKE2b-256 8a9d537f82ccf3e335c4254a200345c41897c67916da44a40228e91f18bae19e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eedeec31b008eef30264ce4dd550c75c7c8adc0d1c076b95855d92eb45f69318
MD5 0d76281b6d2e23b67d593d51e3537a5b
BLAKE2b-256 ca8759a84bb11015ce52aca735d8584d47f35d7f68d60c9395f906fe8c004222

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f94013bca7c19971d34fa9c9363f8789d5f48ad85576733c7b9a19810ad8360a
MD5 3b41a149c27e4bf2d966e58dedf55623
BLAKE2b-256 ee7d798faa6cfef3bb841a2508e5fbb5a7f2a124233037d1f182683b36a321c8

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 438.3 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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6437666611c496e5f24939fcf56642c1c1494ae951521cdeb4a89b45c8a0d3cb
MD5 add28dd430efa56d96fda8e335773555
BLAKE2b-256 54aac23ee1e780211295842322b034d50d5f97dc1fad94e36a4dfdaaa23b5322

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp312-cp312-win32.whl
  • Upload date:
  • Size: 423.4 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.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 5b70ab9844c846bb4742088878d2c9494c1d3796600a8f718e4500f7a5c6e77d
MD5 269e7623d3075a35d9b1f5dce219d043
BLAKE2b-256 90f772db5f79385a876fba1c774b4e253416402992fba715cb8599c36042e840

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 651a5ac2422e57cbb8cd33f4192809b26201e72a59bf609f109000d3517f9996
MD5 45a4e67abf3c63033a6b06c2486d2690
BLAKE2b-256 bbc21b3a75db2f1c40630a45b2335002be1688c9f19d6a87a3bafe11168b6085

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 52fc6e1b5742710f644f0b46cbf0b2211197a4d964bb37176608d29fb989aa59
MD5 e6a8dde321f3cb759b837f35a07e8f46
BLAKE2b-256 9e4c1ce1669ba30141c25cf2fa8a80757f59fec467fe1ba99c840bcf2c57f519

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d0bc7f3a8fbc72ccdd324cc4a70ff3677047b581adeff823959a48915b1e817
MD5 f508da3191c09e79e2369049f126c39e
BLAKE2b-256 8c0125a735ec4054eb2f89f6e7ab0bcc026a0e5566cfe5a85f2ed7faedde04ff

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 440.1 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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 70d6d9a7f522eddc10b322e40530903a53de9e81c53b50dc202da8f58d98ece3
MD5 04e3addb8794eec5db61d6cb39dd6ba3
BLAKE2b-256 f05655d9aaf0c027784807df0955a85956447d421e35aed16896b86c8741bc31

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 425.9 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.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 9d59ff49fc6c8d2091207a1f685056bbf5287eed7e7b93617af4eee5c44ceaed
MD5 5ea6ee6e954c08c7090296d5a5c730c0
BLAKE2b-256 bd89b53bca46893a7b1700b74ddaa34fad2cc7a29f9ae51ad27f8072bfd95822

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 29f1ae725f09cf4a414d10c4a0397ca1ece49b2854e5014ac8997f456f72f9f5
MD5 57353d7b8b6049d7b3b73d4c364a3577
BLAKE2b-256 d2060d20b82bf57c7e28e1d413520ce612c71e7b0351009b0ea5913ed5d2ba7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.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.6.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.6.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fb12a1fef5336f504a9151000d47fae59ec659e4c19ad1f97d1f6fb1fcdb6b73
MD5 e25737457256b04723962674dfbf96f7
BLAKE2b-256 071156ef0aeb17ce518233b6164f705082be7d66a4e1df5ec7a621208909efb0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cbc005db25cf104457a46f681379c38ea087dde9ddb527315c129bd3a1bf866b
MD5 875caf061fdaedd50eb9b65bcf7e1c61
BLAKE2b-256 743e790a0c6bc00ea14e802db69aa9f94752d3019aeb671dac7b251870419243

See more details on using hashes here.

Provenance

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