How can I create .mat file with timeseries in python?

183 次查看(过去 30 天)
Hi!
In my project I want to create a timerseries in python and trigger a simulink simulation with that timeseries as input.
I am not able to create a .mat file in python with a time series. Is this possible?

回答(1 个)

Hassaan
Hassaan 2024-1-16
编辑:Hassaan 2024-1-16
Yes, it is possible to create a .mat file in Python with a time series and then use that .mat file as input data for a Simulink simulation. You can achieve this using the scipy.io library in Python, which provides functions for working with .mat files.
Create and Configure the Time Series in Python: First, you need to create a time series with the required data points and time values. You can use the numpy library for this purpose.
import numpy as np
import scipy.io as sio
# Create time values (e.g., from 0 to 10 seconds with a step of 0.1 seconds)
time_values = np.arange(0, 10, 0.1)
# Create data values for your time series (e.g., a sine wave)
data_values = np.sin(time_values)
# Create a dictionary to store the time series data
time_series_data = {'time': time_values, 'data': data_values}
Save the Time Series as a .mat File:
You can use the sio.savemat function to save the time series data as a .mat file.
# Save the time series data as a .mat file
sio.savemat('timeseries_data.mat', time_series_data)
Use the .mat File in Simulink:In your Simulink model, you can use the "From File" block to read the time series data from the .mat file. Configure the block to read the data appropriately. You can use this data as input for your simulation.Here are the steps to configure the "From File" block in Simulink:
  • Add a "From File" block to your Simulink model.
  • Open the block's properties and set the "File name" to the path of your .mat file ('timeseries_data.mat' in this example).
  • Set the "Variable name" to the name of the time series data (e.g., 'time' or 'data' based on how you organized your .mat file).
  • Connect the output of the "From File" block to the input of the subsystem or block where you want to use the time series data.
Run the Simulink Simulation:Once you have configured the "From File" block and connected it to your model, you can run the Simulink simulation. The data from the .mat file will be used as input to your simulation.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  4 个评论
Baptiste
Baptiste 2024-5-29
Hi,
I've been trying to save Python-generated data into a MATLAB-compatible format.
However, whenever I try and generate an HDF5 file as .mat then open it with MATLAB, I get the following error :
Unable to read MAT-file <file>. Not a binary MAT-file. Try load-ASCII to read as text.
The files are generated as such in Python :
import h5py as h5
import numpy as np
with h5.File(r'file.mat', 'w') as f:
f.create_dataset("dataset", data=data, dtype=np.double)
All datasets are created from two dimensional numpy arrays.
When opening a "native" .mat file with HDFView, it seems that every dataset has a "MATLAB_class" attribute tha contains the value "double". I tried specifying that with :
f['dataset'].attrs['MATLAB_class'] = b'double'
This didn't work either. Any help would be appreciated.

请先登录,再进行评论。

类别

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

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by