Import and Read .mat File in Python
34 views (last 30 days)
i have to load and read a .mat File into Python. The File is an 1x1 double timeseries Output from SImulink wiith "Time" and "Data".
I want do print the Data and Time in the Python console.
I tried a lot but cant figure it out!
mbvoyager on 18 Nov 2022 at 12:27
Edited: mbvoyager on 18 Nov 2022 at 12:30
Let us assume you have this vector in MATLAB:
mu = randn(100,1);
You can save this vector using following command:
Please note that I chose the
option to save the .mat file in an older file format. This can be easily read by python with the following script. Make sure the mu.mat file is inside the same folder as this script:
import scipy.io as sio
muf = sio.loadmat('mu.mat')
mu = muf.get('mu')
mu = mu.flatten()
There are other questions regarding the standard .mat file format from MATLAB, this you can find here: StackOverflow read .mat files. Here someone suggested to use a custom written package for python called 'mat73'.
In general .mat files are an variation of the well known HDF file format so with the latest version of a HDF5 reader and the latest .mat file version, there are very likely other options to import .mat files in different programming languages than the one that I present here.