Helpful functions for loading .mat MATLAB files into Python. Most data types are supported.
Project description
readmat
readmat is a Python package with helpful functions for loading .mat MATLAB files into Python. Most data types are supported.
Installation:
pip install readmat
Prerequisites:
python 3
python modules:
- scipy
- numpy
Functions:
load(filename, isNumber=False, isCharArray=False, isStruct=False, isFunction=False, isArray=False, isMatrix=False, isBool=False, isInf=False, isNaN=False, isFunctionHandle=False)
- Example:
matdata1 = readmat.load('matfile1.mat', isCharArray=True)
- Usage depends on data type. For structs, an example usage is
matdata1.this.is.a.test
. For all other data types,matdata1
. - Input: .mat file
- Output: depends on data type
mat2obj(filename)
- Example:
matdata2 = readmat.mat2obj('matfile2.mat')
- Example Usage:
matdata2.this.is.a.test
- Input: .mat file
- Output: object
mat2dict(filename)
- Example:
matdata3 = readmat.mat2dict('matfile3.mat')
- Example Usage:
matdata3['this']['is']['a']['test']
- Input: .mat file
- Output: dictionary
options(matobj)
- Example:
readmat.options(matdata1)
- Example:
readmat.options(matdata2)
- Input: object
- Output: list of options
How structs are loaded:
The load function loads your MATLAB structure as an object in Python. What does this mean? Basically, the method of exploring the depths of your struct in Python is similar to what you'd do in python. Pretty neat examples below:
MATLAB | Python |
---|---|
DWI.dat(1,1,1) | myMat.DWI.dat[0][0][0] |
DWI.hdr.private.hdr.test.hello | myMat.DWI.hdr.private.hdr.test.hello |
lesion_jhu.mean | myMat.lesion_jhu.mean |
data.grades(101) | myMat.data.grades[100] |
Known limitations:
The following MATLAB data types are not supported:
- string arrays (for example: "example" [notice the pair of double quotes])
- datetime
- categorical
- table
- timetable
Tips for overcoming each limitation:
(MATLAB commands listed below)
- convert string arrays to char arrays, command:
char(stringarray)
- convert datetime to either a char array or a struct, command:
char(vardatetime)
orstruct(vardatetime)
- convert categorical to double, command:
double(varcategorical)
- convert table to cell array, command:
arrayfun(@convertStringsToChars,arrayfun(@string,[vartable.Properties.VariableNames;table2cell(vartable)]),'UniformOutput',false)
- convert timetable to cell array, command:
horzcat(vertcat('Time',cellstr(char(vartimetable.Properties.RowTimes))), arrayfun(@convertStringsToChars,arrayfun(@string,[vartimetable.Properties.VariableNames;table2cell(vartimetable)]),'UniformOutput',false))
Sources cited:
Although I did write part of readmat's code , without the amazing stackoverflow answers from the following stackoverflow users, this project would not have be possible:
users cs01 and jpapon: https://stackoverflow.com/a/29126361
user andyvanee: https://stackoverflow.com/a/6573827
License
This software includes a MIT license.
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 Distributions
Built Distribution
File details
Details for the file readmat-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: readmat-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98f5c6f8a5430e62c4388d2537646a40f689813362808322390881f022680d97 |
|
MD5 | 5e5a12d1beb9cef2487af7bdb45c0fb3 |
|
BLAKE2b-256 | abb1529e0c1fc79ec6c0fc94d47616eadb3e9ccda744fad52f740d5cc42511a6 |