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
  • categorical
  • Enumeration Instance Arrays

Data is returned in the same format as scipy.io.loadmat, i.e., as a dictionary of {var_name: var_data}.

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.

Supported MAT-file versions are v4, v6, v7 and v7.3.

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, 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-1.0.0.tar.gz (594.2 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-1.0.0-cp313-cp313-win_amd64.whl (439.3 kB view details)

Uploaded CPython 3.13Windows x86-64

mat_io-1.0.0-cp313-cp313-win32.whl (425.6 kB view details)

Uploaded CPython 3.13Windows x86

mat_io-1.0.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-1.0.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-1.0.0-cp313-cp313-macosx_11_0_arm64.whl (452.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mat_io-1.0.0-cp312-cp312-win_amd64.whl (440.3 kB view details)

Uploaded CPython 3.12Windows x86-64

mat_io-1.0.0-cp312-cp312-win32.whl (425.9 kB view details)

Uploaded CPython 3.12Windows x86

mat_io-1.0.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-1.0.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-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (454.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mat_io-1.0.0-cp311-cp311-win_amd64.whl (442.2 kB view details)

Uploaded CPython 3.11Windows x86-64

mat_io-1.0.0-cp311-cp311-win32.whl (428.4 kB view details)

Uploaded CPython 3.11Windows x86

mat_io-1.0.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-1.0.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-1.0.0-cp311-cp311-macosx_11_0_arm64.whl (455.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: mat_io-1.0.0.tar.gz
  • Upload date:
  • Size: 594.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mat_io-1.0.0.tar.gz
Algorithm Hash digest
SHA256 168b6887866b3720ebeaf74ee318a2d1fda26125c8faa11fbb5efa0d4dbdeb1a
MD5 1b1deea8d2dd2d0f0feacfb88d6c92cf
BLAKE2b-256 8a37b17c6e93fc339817346823f0f54de746e885d5eb942235bfa40818a47c06

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 07f8d1603aebe7d80f01fee9ed15d337016e184b9280b8dd5f5964555ff5b33d
MD5 374d090e36a311ea670eb45123a1d894
BLAKE2b-256 7e04912fa6b448d29223be2bef79ac1cc4842d646aec86d305c3d9d75a4c3266

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 66e52ca181c71f31f18bc73324e6d9b0eb73db82a165e8d62d11b30baba82b0c
MD5 0177ab513e79f0fbe920b97633ba8a82
BLAKE2b-256 070129ee802d1a1491bbd102ea716473e7fb0d9db39e5ba1ceb022a22f7a8b2a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4afa61f663e0de08c1d09b7f4fecdd71df806db621cb86df1245a33fe7365162
MD5 95dae0453658621a7989a073ac8616f7
BLAKE2b-256 8496230ae3d3a306851caae51332716f8479ea704ebac8954b6cd06def236424

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-1.0.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-1.0.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-1.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 452b728a6c8183754a011bde78781fa8dc660e27adc2725fb066b371babf418f
MD5 55389421b408d15a8f9354ac92872704
BLAKE2b-256 74ad3684072c0152f90440d44c8376c975a6f143a7b0cc55c114771ee9717c41

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 280b9d1d465ebce67e0e2b11142e8f6d1be5fa651918c0bfdc15237b1e6a3077
MD5 3946fb4e42a4981e4554cb1cedac1d57
BLAKE2b-256 311f3fbe564149332ee9639ba385d66807550929fd3a86410a3d66d58761e6e2

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 11361e8b16c329295e898cab37bae7dc6bc06b370d3b774ac377609d9fe889b6
MD5 fadd5c835a9024ef83f52fe8dd73ded0
BLAKE2b-256 539cccec6457fd130e2602658b8e3a04bd0c1b6192a9298d58e5b4a4be53ff6c

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 c6ebaf90d3dadfbfa47b26147521d298060ddae8203f582f8cf22a66ed54eab6
MD5 50e7e7c5d039564605188952432d0d47
BLAKE2b-256 e2c7f02c0467d9b540c0e1ce0b6539644fd61ed185b964a0e3495366321407e3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4a7c40ef1c8e7a503904f3a5aef93360458d94f76cacee4116ba078071f3c7f5
MD5 881a4bbfd0c216f24cc2ed25321dd2cd
BLAKE2b-256 371cffebb520f8f15b866d8235d8ef97c41ffa594ae70dee26ae898b85e345c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-1.0.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-1.0.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-1.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 376109e48149f6c2020b605f812ce88eff6a5dbd7ea43a05211ba11a648c0a0f
MD5 37b5b3da59166faf54a36b2793b1153a
BLAKE2b-256 4bbc149e6c584135172e91fbf176957af62ba6620752271ceda2079fef5c9747

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0490198ca4662b4765f8f77cb672f2954fda47f38e515081b24c5dec8fbcf589
MD5 c00fc7a6104b19cbcf45a931070048a3
BLAKE2b-256 594f61c5dd2e7eac7d9b78fe1c113651472f70691f5dd0e08f5f973c7c59994b

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1d05fb60448100e060c79d6f86820974d85c1efd7224eabf67d8f7e6dd0af02f
MD5 c2b4cd3d19b2a468b337b4975ad0aab9
BLAKE2b-256 fdad27ee887835f0889e8070e6c3ea25ce43b075c1ad12b5535c3cf252938981

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for mat_io-1.0.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 d681dcee209543978b58f4e12a09f359c37ea001fe92913288ae521402df720c
MD5 dbcf0e4d2d4ab7069d7ccf7b66d5bc5a
BLAKE2b-256 a4ebd6154f3bb42e32294dbb4bb68aa856e08cd70a6e13e8f78541627ac9d7f9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1358963c797cfc7791e0a29ea3391838949c49f32b5b2110035d4d2a84a27641
MD5 dbce6944556a3faa1968996d7789496a
BLAKE2b-256 c2113aaa3cce892d77f3c4a60f9899f6b57af2ac7bbd83ec53a9ca4cc1270827

See more details on using hashes here.

Provenance

The following attestation bundles were made for mat_io-1.0.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-1.0.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-1.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 22aab7814322caeae47b5a58aa9e65a287bedede793e838673c3159f1e852061
MD5 24af87542cf2d3db1aa1cef8d75f42a0
BLAKE2b-256 3b28beafc397c6ad614b2023e7c9e25ad1dd034c6f412d118a8a2d0573f8e1f5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mat_io-1.0.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0c9a8cf151f413fd0f567ec72fa06a1161a9450cbc2a2a458d400984d5bc86c4
MD5 e5342e4b9f0e8c4a1c28f0ca452f093c
BLAKE2b-256 46011c7aa1b1e78e0c655841e37a1344c91178e6338ae88c32817390d57e7feb

See more details on using hashes here.

Provenance

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