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

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

Shivam garg
Shivam garg 2024년 1월 16일
편집: Shivam garg 2024년 1월 16일
I am getting following error when I try this:
  • Simulink does not support loading the input data in file 'timeseries_data.mat'. For data saved using MAT file versions prior to 7.3, Simulink can only load two-dimensional arrays consisting of one-dimensional, double, noncomplex samples. To load data of any other type, complexity or dimension, use a timeseries object and save the file using MAT file version 7.3 or later. For example, use: 'save file_name -v7.3 timeseries_object'.
The Scipy library says :
You will need an HDF5 Python library to read MATLAB 7.3 format mat files. Because SciPy does not supply one, we do not implement the HDF5 / 7.3 interface here.
Hassaan
Hassaan 2024년 1월 16일
편집: Hassaan 2024년 1월 16일
Since you're using scipy.io.savemat which saves the .mat file in a format that Simulink might not recognize if the data is not a two-dimensional double array, you're encountering this issue. Also, the newer MATLAB 7.3 file format is based on HDF5, and as the error message suggests, you need to save the file in this format for Simulink to correctly read complex or non-double data.
some recommendations:
Ensure Data Type and Shape: Make sure your data is a two-dimensional array of doubles. Even if you have one-dimensional data, it should be shaped as a two-dimensional array with one column and as many rows as there are data points.
Use MATLAB to Create a timeseries Object: If you have access to MATLAB, the best way to handle this is to load your .mat file into MATLAB, convert the data into a timeseries object, and then save it using the -v7.3 option.Here's an example of how you would do this in MATLAB:
load('timeseries_data.mat'); % Load your .mat file
ts = timeseries(data, time); % Create a timeseries object
save('timeseries_data_v73.mat', 'ts', '-v7.3'); % Save in MATLAB 7.3 format
You would then use 'timeseries_data_v73.mat' in your Simulink model.
Use h5py to Save in HDF5 Format: If you only have Python and cannot use MATLAB to create a timeseries object, you can use the h5py library to save the .mat file in HDF5 format which can be read by MATLAB 7.3.First, you need to install h5py if you haven't already:
pip install h5py
Then you can use the following Python code to save your data:
import numpy as np
import h5py
# Your time series data
time_values = np.arange(0, 10, 0.1)
data_values = np.sin(time_values)
with h5py.File('timeseries_data_v73.mat', 'w') as f:
# Create datasets for time and data
f.create_dataset('time', data=time_values)
f.create_dataset('data', data=data_values)
# Optionally, if you need to save the data in a struct like MATLAB
# you would create a group and then create datasets within that group
When using the file in Simulink:
- Point the "From File" block to `'timeseries_data_v73.mat'`.
- Make sure to use the variable name that matches the datasets created within the HDF5 file. In the Python code above, those names are `'time'` and `'data'`.
By following these steps, you should be able to create a .mat file that Simulink can read without issues. Remember that the Simulink "From File" block expects the data to be in specific formats, and using the MATLAB 7.3 HDF5-based format gives you more flexibility in terms of the types of data you can load.
------------------------------------------------------------------------------------------------------------------------------------------------
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.
How can I create .mat file with timeseries in C#?
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 1월 16일

댓글:

2024년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by