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.2.tar.gz (583.4 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.2-cp313-cp313-win_amd64.whl (439.2 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-0.6.2-cp313-cp313-win32.whl (424.9 kB view details)

Uploaded CPython 3.13Windows x86

mat_io-0.6.2-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.2-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.2-cp313-cp313-macosx_11_0_arm64.whl (453.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-0.6.2-cp312-cp312-win_amd64.whl (440.1 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-0.6.2-cp312-cp312-win32.whl (425.2 kB view details)

Uploaded CPython 3.12Windows x86

mat_io-0.6.2-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.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (454.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-0.6.2-cp311-cp311-win_amd64.whl (441.9 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-0.6.2-cp311-cp311-win32.whl (427.7 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-0.6.2-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.6.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (455.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-0.6.2.tar.gz
  • Upload date:
  • Size: 583.4 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.2.tar.gz
Algorithm Hash digest
SHA256 e321f1225cf0604e9fe39a9e1c8d52c100d73c8e16628d9e198e2564371a776c
MD5 0b1d4a6f8ee47b69979854a8c7b8ffec
BLAKE2b-256 2721761fe3463a89c59ca9fb287d98536cac75ac1d53b546a012ce6b453fc3c4

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 439.2 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 98f38df3015179671288e1bd0256192cd67c915beae242d10bf346730c1afb67
MD5 42c751079a4f297442a413e2ba74f865
BLAKE2b-256 5f36c333742af686194a72fb89c17759ab1dedfa08c38fce7600441e9cc3dc7a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp313-cp313-win32.whl
  • Upload date:
  • Size: 424.9 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.2-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 8880ff729089ada68f41d4493c59bad9550d39709b2304b2b17092722d4bf724
MD5 10c43cf6a6837713eaad75ee2327f632
BLAKE2b-256 b3fa82bf2e3fd411be751cc943f545e01dbc570fd4195bcad7f79216d66c5082

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 881b72a7e324bc6ba877d8cb68c41c5bdac28c9bb1ac703be312343fbc5360f1
MD5 8e1ad9e0ffd39eb6647968161a85ca2c
BLAKE2b-256 b2034151a3b5842760c853c8cbf63dfcb4a28a48571c76357109bed03add47c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.2-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.2-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.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4d33b086326436e00d7b3274c1da35ecb8b0135aeb0554523c53ef76a03990bd
MD5 91b66e52197ab81238098ac3ef400c32
BLAKE2b-256 d65901c2e10894801906982bcacd1d84ed91e8133fa328e32e51284265a16275

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6675ac1d4d4cfd1a5cbde3d174d47c2c5a5e4566c7a9a89afb15b7a174b61bde
MD5 3597e5ea891a0dcf16c33882a10652f6
BLAKE2b-256 f17468d8e4b0d285568b104a08a64ea9c11279a2943ee8922fe5b40b98079ce9

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 440.1 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4c09b263e302dcdd99015f69b7a1b90695474cd6e7b5e5fef40a23601f1009b7
MD5 0c0b3b7fd5c57c5d068953ff7570fd61
BLAKE2b-256 880b3d6c6b0f642a746620962ce22360e1f18be17a21067d38fef49455b188e2

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp312-cp312-win32.whl
  • Upload date:
  • Size: 425.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.6.2-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 58649a7d0b2804b5375a44090751956dc547d9996f043594dcf928c938a81881
MD5 4933ccae08282641bfa6e8deddb244b0
BLAKE2b-256 14fb3507ed007d3c1ff346d5baa645cf82ec033b68ea3afafc65c08526aecca5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 aa1f1a9f83d695e0a157a6ecdf45c50e5e95f018a3c743417f7bda9832e8d594
MD5 81e1b449efa9571d55b9aa0cd0072ccf
BLAKE2b-256 afa520eab12441abd15686251e829ba249668eaa2cd64358a679e0457174cfd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.2-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.2-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.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d8aedeb0c5e21dd0c34fed5aab75c127f73eb686a7939a5f3746e1411e9e3269
MD5 d71ed169e5aeeeb86dd6c4ed964481f4
BLAKE2b-256 a4a3b27aaaf6ec5fa552f62b7b5d81e55bbc94c5552e612b460497812858d76c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 917d1aba744cb6bda64c21619662342816795a6bfdc560a0d449f3234c0407e2
MD5 3cd1162aee46b0d86e0ca3be4219d77a
BLAKE2b-256 633795d9159f39c5e20a55fff822d7f5a4ff50a16ce581feba08f2eec07b0ec0

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 441.9 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c11e2e48a5d3829cd6c9bfd5ba6bfc70b12b173685737f2e28c95cec1373b0c0
MD5 5f45fd639cebc29f5978f45d819b41dc
BLAKE2b-256 98d6873733240c54ada612adc7475ba0c4f914f11ba5200a4c7a6be8f4b3928a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mat_io-0.6.2-cp311-cp311-win32.whl
  • Upload date:
  • Size: 427.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.6.2-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 7010d890b92ea01cf72895ed6c30eeeda020a85a74dea5125197746b1afec299
MD5 f50871e88ecbe02075d327fb80c55b1b
BLAKE2b-256 ae31315888111a1626b9ea1d2f4b825abc9e25a6dbe1572426929e342e21fa68

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ddfb9788f34335090ae55a2a21864498d2486f476351ece702ec592708b95749
MD5 642a7533ee74562761c77bc0f194a2a2
BLAKE2b-256 20c29ba441e324c82d1e7482b0c0d319c07609af9fa7be5fed74bfd4cf63a2a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-0.6.2-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.2-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.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eff6d36e1d800b3abba2461cce944903a0c51d9733e4e6d222e8bed71fc57223
MD5 d0d2f39ed0f09e6130ee03bb19afacd7
BLAKE2b-256 bb7119d4ea5ad22d357c0bfc67494b153c381dd54828b04ae0645770164ff8c3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-0.6.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 65b0cd50ce10924a3c6af10709b45514463b966abd9d9132a4dedaa1bd3fc66f
MD5 53010bfe8686f969ade4bf2933dae0b1
BLAKE2b-256 8297332842ab097e98fc776b3751fb6b66e257340355102e426c79316a4d8f05

See more details on using hashes here.

Provenance

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