Import and Read .mat File in Python

973 次查看(过去 30 天)
Nick Froidl
Nick Froidl 2021-2-9
评论: mbvoyager 2022-11-18
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!
  1 个评论
svanimisetti
svanimisetti 2022-11-18
I am looking for similar functionality. Have you found an answer yet?

请先登录,再进行评论。

回答(1 个)

mbvoyager
mbvoyager 2022-11-18
编辑:mbvoyager 2022-11-18
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.
  2 个评论
svanimisetti
svanimisetti 2022-11-18
Thanks for the quick reply @mbvoyager. Does this also work for .mat/mldatx files that contain TimeSeries data? This would typically be output/log from Simulink runs. I tried to use 'hf5py' module, but was unable to read TimeSeries data stored in a .mat file. Scipy seems to work well for cases when Maltab workspace is saved to .mat file.
mbvoyager
mbvoyager 2022-11-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by