Import and Read .mat File in Python

321 visualizaciones (últimos 30 días)
Nick Froidl
Nick Froidl el 9 de Feb. de 2021
Comentada: Sebastian el 24 de Jul. de 2025
Hello,
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!
  2 comentarios
svanimisetti
svanimisetti el 18 de Nov. de 2022
I am looking for similar functionality. Have you found an answer yet?
Sebastian
Sebastian el 24 de Jul. de 2025
Same here. After long random searching I found the values of our time series in
mat_data = hdf5storage.loadmat(mat_file_path)
mat_data['#subsystem#'][0]['MCOS'][0, 20]
but did not find the time stamps linked to it.
Saving the file as 'v7' and loading with scipy.io.loadmat I did not even manage to find the values of the time series.
I would apreciate if Mathworks support could provide a solution how to retrieve simulink time series the HDF5 .mat files.

Iniciar sesión para comentar.

Respuestas (1)

mbvoyager
mbvoyager el 18 de Nov. de 2022
Editada: mbvoyager el 18 de Nov. de 2022
Let us assume you have this vector in MATLAB:
mu = randn(100,1);
You can save this vector using following command:
save('mu.mat','mu','-v4')
Please note that I chose the
'-v4'
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()
print(mu)
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.
  3 comentarios
mbvoyager
mbvoyager el 18 de Nov. de 2022
can you provide some test data? E.g. a small dummy file representing the data structure you are using? I do not work with the TimeSeries data format.
Viren
Viren el 18 de Jun. de 2025
Thanks @mbvoyager

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by