Thursday, January 13, 2011

Reading and Save Mat Data in Python

Reading and writing .mat files with Python
If you have some old data, or find some interesting data set online, stored in Matlab's .mat file format it is possible to read this using the scipy.io module in SciPy. This is how to do it.

data = scipy.io.loadmat('test.mat')
"data" now contains a dictionary with keys corresponding to the variable names saved in the original .mat file. The variables are in array format. Saving to .mat files is equally simple. Just create a dictionary with all variables you want to save and use savemat().
data = {}
data['x'] = x
scipy.io.savemat('test.mat',data)

No comments:

Post a Comment