There is a nice package called mat4py
which can easily be installed using
pip install mat4py
It is straightforward to use (from the website):
Load data from a MAT-file
The function loadmat
loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict
and list
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 the JSON format.
Example: Load a MAT-file into a Python data structure:
from mat4py import loadmat
data = loadmat('datafile.mat')
The variable data
is a dict
with the variables and values contained in the MAT-file.
Save a 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 for loadmat
, i.e. it should be composed of simple data types, like dict
, list
, str
, int
, and float
.
Example: Save a Python data structure to a MAT-file:
from mat4py import savemat
savemat('datafile.mat', data)
The parameter data
shall be a dict
with the variables.