Does anyone have successful experience reading 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)
There is a nice package called
mat4py
which can easily be installed usingIt is straightforward to use (from the website):
Load data from MAT-file
The function
loadmat
loads all variables stored in the MAT-file into a simple Python data structure, using only Python’sdict
andlist
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 theJSON
format.Example: Load a MAT-file into a Python data structure:
The variable
data
is adict
with the variables and values contained in the MAT-file.Save 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 forloadmat
, i.e. it should be composed of simple data types, likedict
,list
,str
,int
andfloat
.Example: Save a Python data structure to a MAT-file:
The parameter
data
shall be adict
with the variables.Neither
scipy.io.savemat
, norscipy.io.loadmat
work for matlab arrays --v7.3. But the good part is that matlab --v7.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.I've screwed half an hour even after reading the answers. Hope this answer helps
First save the mat file as
After that in Python use the usual loadmat
Reading the file
Insecting the type of mat variable
The keys inside the dictionary are matlab variables and the values are the objects assigned to those variables.
Silly me. Forgot to import io...
for high dimensional data, mat4py package works better: