Read .mat files in Python Read .mat files in Python python python

Read .mat files in Python

An import is required, import

import scipy.iomat ='file.mat')

Neither, nor 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 npimport h5pyf = h5py.File('somefile.mat','r')data = f.get('data/variable1')data = np.array(data) # For converting to a NumPy array

First save the .mat file as:

save('test.mat', '-v7')

After that, in Python, use the usual loadmat function:

import as siotest = sio.loadmat('test.mat')