Read .mat files in Python
Solution 1
An import is required, import scipy.io
...
import scipy.io
mat = scipy.io.loadmat('file.mat')
Solution 2
Neither scipy.io.savemat
, nor scipy.io.loadmat
work for MATLAB arrays version 7.3. But the good part is that MATLAB version 7.3 files are hdf5 datasets. So they can be read using a number of tools, including NumPy.
For Python, you will need the h5py
extension, which requires HDF5 on your system.
import numpy as np
import h5py
f = h5py.File('somefile.mat','r')
data = f.get('data/variable1')
data = np.array(data) # For converting to a NumPy array
Solution 3
First save the .mat file as:
save('test.mat', '-v7')
After that, in Python, use the usual loadmat
function:
import scipy.io as sio
test = sio.loadmat('test.mat')
Solution 4
There is a nice package called mat4py
which can easily be installed using
pip install mat4py
It is straightforward to use (from the website):
Load data from a MAT-file
The function loadmat
loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict
and list
objects. Numeric and cell arrays are converted to row-ordered nested lists. Arrays are squeezed to eliminate arrays with only one element. The resulting data structure is composed of simple types that are compatible with the JSON format.
Example: Load a MAT-file into a Python data structure:
from mat4py import loadmat
data = loadmat('datafile.mat')
The variable data
is a dict
with the variables and values contained in the MAT-file.
Save a Python data structure to a MAT-file
Python data can be saved to a MAT-file, with the function savemat
. Data has to be structured in the same way as for loadmat
, i.e. it should be composed of simple data types, like dict
, list
, str
, int
, and float
.
Example: Save a Python data structure to a MAT-file:
from mat4py import savemat
savemat('datafile.mat', data)
The parameter data
shall be a dict
with the variables.
Solution 5
Having MATLAB 2014b or newer installed, the MATLAB engine for Python could be used:
import matlab.engine
eng = matlab.engine.start_matlab()
content = eng.load("example.mat", nargout=1)
Gilad Naor
Current: Facebook Engineering Manager in Content Integrity. Previous: Amazon · Bright Source Energy · Intel · Quasars Demo Group
Updated on May 27, 2021Comments
-
Gilad Naor almost 3 years
Is it possible to read binary MATLAB .mat files in Python?
I've seen that SciPy has alleged support for reading .mat files, but I'm unsuccessful with it. I installed SciPy version 0.7.0, and I can't find the
loadmat()
method. -
texnic almost 10 yearsscipy does not support v7.3 mat-files (see notes here). See the answer by vikrantt for solution.
-
watsonic about 9 yearshowever, you can save mat-files as earlier versions. see: mathworks.com/help/matlab/import_export/mat-file-versions.html (header: 'Save to Nondefault MAT-File Version')
-
watsonic about 9 yearse.g.
save('myfile.mat','-v7')
-
chipaudette almost 9 yearsThis works fine, if you use the '-v7.3' flag in Matlab when saving out your data. Using the default
save
(at least in Matlab R2014b) results in a file that cannot be read using the technique above. If you do use the '-v7.3' flag, the numeric data can be read just fine. -
vikrantt almost 9 yearsYes, that's what I said in my post. You need to use -v7.3 while saving in Matlab. You should do that anyways as it uses a better/more supported/standardized format.
-
Daniel over 8 yearsThat's a somehow crazy documentation provided by mathworks. 40 pages explaining the format, without mentioning that it is a subset of HDF5.
-
heracho almost 7 yearsCould you please explain what is the relation between f and data in your example? How can I move f to a numpy array?
-
Kevin Katzke almost 7 yearsSave a variable with this command from the prompt:
save('filename', '-v7.3', 'var1');
-
VimNing almost 6 yearsI got this error: ModuleNotFoundError: No module named 'pylab'.
-
Daniel almost 6 yearsYou got the error when trying this answers? That is odd, it does not use pylab.
-
denis over 5 yearsNote that mat4py gives you a json-like tree of dicts, lists, lists of lists ... -- no numpy at all. (
mat4py/cmd.py my.mat
writesmy.json
, 1 long line.) -
Cleb over 5 years@denis: Yes, that's also stated above. But a good point indeed: I usually like this structure, e.g. in web applications as numpy arrays are not JSON serializable.
-
s2t2 almost 5 yearsEncountered:
mat4py.loadmat.ParseError: Can only read from Matlab level 5 MAT-files
-
Cleb almost 5 years@s2t2: never ran into this issue before. What matlab version and which scipy version are you using?
-
Aleksejs Fomins about 4 yearsParseError: Unexpected field name length: 43
-
Cleb about 4 years@AleksejsFomins: Probably best to open a new question and link to this thread; then it will be easier to help (make sure to provide all info needed to reproduce the error).
-
devspartan almost 4 yearsHow would i even know that it contains data under data/variable1 ??
-
Chachni almost 4 years@s2t2 mat4py cannot read the new matlab format from version 7.3, which is encoded as hdf5.
-
Packard CPW over 3 years@devSpartan
f.keys()
will show you what you can access -
Ramin Melikov over 3 years"Unable to open file (file signature not found)"
-
ThatNewGuy about 3 years@heracho f is the file object that lets you read data from the hdf5 file. It's similar to using
f = open('myfile.txt')
to read a text file. -
ThatNewGuy about 3 years@vikrantt This is a great solution for reading a
struct
but it does not work for a Matlabtable
. To read tables, you can't use thesave
function. Instead, you need to utilize theh5create
andh5write
functions. -
emilaz almost 3 yearsAt least for
.mat
files generated with MATLAB, this will result in aUnicodeDecodeError
. -
MrCrHaM almost 3 years@emilaz This is expected.
np.loadtxt
is only meant for.mat
files generated by Octave. -
emilaz almost 3 yearsYes, I just put it there as clarification for future people looking at this :)
-
SjonTeflon over 2 yearsAlso note that mat4py does not allow to read complex valued .mat files
-
nKandel about 2 yearsThe 2 things that it did better are: it preserve the mat object dimension and the loaded object was in NumPy array. Thanks
-
Abrar_11648 about 2 yearsI faced this issue: "mat4py.loadmat.ParseError: Can only read from Matlab level 5 MAT-files"
-
panter about 2 yearsI'm glad it helped :)
-
ZaydH about 2 yearsUpdated link to the SciPy.io tutorial docs.scipy.org/doc/scipy/tutorial/io.html @FranckDernoncourt
-
LightCC almost 2 yearsFor the record, this answer requires a valid Matlab installation and license - it runs Matlab in the background to accomplish the read. And there may be limitations on what format you get the items in that need further work to make them readable. For example, Simulink.Bus objects come in as a "matlab object" and must be processed further, with issues if you want to extract the Bus Element objects.