Save Run-Time Data from Simulation

6 visualizzazioni (ultimi 30 giorni)
Gustavo Araujo
Gustavo Araujo il 18 Dic 2020
Modificato: Anay il 20 Feb 2025
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.

Risposte (1)

Anay
Anay il 20 Feb 2025
Modificato: Anay il 20 Feb 2025
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!

Categorie

Scopri di più su Simulink Coder in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by