matlab – Read .mat files in Python

matlab – Read .mat files in Python

An import is required, import scipy.io

import scipy.io
mat = scipy.io.loadmat(file.mat)

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

matlab – Read .mat files in Python

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)

Leave a Reply

Your email address will not be published. Required fields are marked *