load .mat file from python

6.8k Views Asked by At

I am trying to run from Python a script in Matlab that run a Simulink mode, save a variable as Power.mat and read this variable in Python. I am using Python 2.7 on Windows.

I've tried to use the library hdf5storage to read the file:

import hdf5storage
x=hdf5storage.loadmat('Power.mat','r')

but I get the error attached. error

Which could be the problem? I have also tried with the library h5py but I get the same error. The file .mat seems not to be corrupted since I open it without any problem in Matlab.

Thanks!

2

There are 2 best solutions below

1
minhduc On

You can use scipy.io to exchange data between Python and Matlab. There are functions named savemat and loadmat for this purpose.

Something like this should work:

import scipy.io
mat = scipy.io.loadmat('Power.mat')

For reference, http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

1
RVK On

Try this code :

import h5py
Data = h5py.File('File.mat')