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 reading .mat files, particularly for extracting contents from user-defined objects or MATLAB datatypes such as datetime, table and string. It uses a wrapper built around scipy.io to extract raw subsystem data from MAT-files, which is then parsed and interpreted to extract object data.
mat-io can read almost all types of objects from MAT-files, including user-defined objects. Additionally, it includes utilities to convert the following MATLAB datatypes into their respective Pythonic objects:
stringdatetime,durationandcalendarDurationtableandtimetablecontainers.Mapanddictionarycategorical- Enumeration Instance Arrays
Note: load_from_mat() uses a modified fork of scipy. The fork currently contains a few minor changes to scipy.io to return variable names and object metadata for all objects in a MAT-file. This change is available on Github and can be installed directly from the branch. You can also view the changes under patches/scipy_changes.patch and apply it manually. Note that you might need to rebuild as parts of the Cython code was modified. Follow the instruction on the official SciPy documentation.
Usage
Install using pip
pip install mat-io
Example
To read subsystem data from a .mat file:
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)
print(data)
Parameters
-
file_path:strFull path to the MAT-file. -
raw_data:bool, optional- If
False(default), returns object data as raw object data - If
True, converts data into respective Pythonic datatypes (e.g.,string,datetimeandtable).
- If
-
add_table_attrs:bool, optional IfTrue, additional properties of MATLABtableandtimetableare attached to the resultantpandas.DataFrame. Works only ifraw_data = False -
mdict:dict, optional Dictionary into which MATLAB variables will be inserted. IfNone, a new dictionary is created and returned. -
spmatrix:bool, optional (default =True) Whether to read MATLAB sparse matrices as SciPy sparse matrixcoo_matrix. -
**kwargs: Additional keyword arguments passed toscipy.io.loadmat. These include:byte_ordermat_dtypechars_as_stringsmatlab_compatibleverify_compressed_data_integrityvariable_namesuint16_codec
MATLAB objects
MATLAB objects are returned as a dictionary with the following fields:
_Class: The class name_Props: A structurednumpy.ndarraycontaining the property names and their contents. Dimensions are determined by the object dimensions.
If the raw_data parameter is set to False, then load_from_mat converts these objects into a corresponding Pythonic datatype. This conversion is detailed here.
Contribution
There's still lots to do! I could use your help in the following:
- Reverse engineer the MAT-file structure to include support for more objects like
categorical,calendarDurationand others - Write object data into MAT-files
- Write tests
- Algorithmic optimization to integrate within the
scipy.ioframework - Documentation of the MAT-file format
Feel free to create a PR if you'd like to add something, or open up an issue if you'd like to discuss! I've also opened an issue with scipy.io detailing some of the workflow, as well as a PR to develop this iteratively. Please feel free to contribute there as well!
Acknowledgement
Big thanks to mahalex for their detailed breakdown of MAT-files. A lot of this wouldn't be possible without it.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mat_io-0.2.0.tar.gz.
File metadata
- Download URL: mat_io-0.2.0.tar.gz
- Upload date:
- Size: 106.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4358118c116e7620967e163ed14e6867f7419f27e3803f6c46970d63d6a08c0
|
|
| MD5 |
dde7536f810f74bf407a4d93160dc8aa
|
|
| BLAKE2b-256 |
73436c38c028d66f9b0c396521bb5e476f5ea892febbc8f59417c10876d478cb
|
File details
Details for the file mat_io-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mat_io-0.2.0-py3-none-any.whl
- Upload date:
- Size: 138.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84ef70642069cd92540451b8e677877a90d355c4bf4093d76167b0b928c3716e
|
|
| MD5 |
59918f425f54d5fe4b027e067f326f04
|
|
| BLAKE2b-256 |
dd94b8d3f8873b66b7365028c9f751e488b6e550b21483030053b667feff636f
|