Save Run-Time Data from Simulation

9 vues (au cours des 30 derniers jours)
Gustavo Araujo
Gustavo Araujo le 18 Déc 2020
Modifié(e) : Anay le 20 Fév 2025 à 5:48
Hello everyone!
I'm running a simulation and exporting the data to workspace. But when a new simulation starts, I need to save the data generated over again. Is there a way to do this automatically? I mean, when I run a simulation at Simulink, when it finishes Matlab asks me where I want to save the specified data or put a great simulation time and each time step that I want, generate and save the data that I want to export while the simulation is running.
Thank you all in advance.

Réponses (1)

Anay
Anay le 20 Fév 2025 à 5:47
Modifié(e) : Anay le 20 Fév 2025 à 5:48
Hi Gustavo,
I understand that you want the output of your simulation to be saved automatically before running it again.
You can achieve this using the “StopFcn” callback. In the “StopFcn” callback, you can specify a script which you want to run after the simulation stops. You can follow these steps to set the “StopFcn” callback for your model:
  1. Go under the “Modelling” tab, expand the “Model Settings” drop-down to find “Model Properties.
  2. In the “Model Properties” dialog box, navigate to “Callbacks” tab and select “StopFcn”.
  3. Enter your MATLAB code which you want to run (in your case code to save the model output).
You can use the below code as an example to write your callback:
% Load existing data
if isfile('output_data.mat')
load('output_data.mat', 'appended_data');
else
appended_data = [];
end
% Append new data
appended_data = [appended_data; out];% out->output data of the simulation
% Save updated data
save('output_data.mat', 'appended_data');
Adding this code as “StopFcn” callback appends the output data of your simulation to a “.mat” file after simulation stops.
I hope this solves the issue!

Catégories

En savoir plus sur Model, Block, and Port Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by