Accessing Simulink model inputs and outputs when stepping model via MATLAB set_param(mdl, 'SimulationCommand', 'step') command
Ältere Kommentare anzeigen
I am programmatically stepping a Simulink model simulation using the set_param(mdl, 'SimulationCommand', 'step') command.
I am using this approach, not the sim() function, because I want to 1) monitor model states and outputs at intermediate time steps, 2) change model input values manually at intermediate time steps. Here is the code layout:
while get_param(mdl, 'SimulationTime') < t_stop
if eq(get_param(mdl, 'SimulationTime'), t_start)
set_param(mdl, 'SimulationCommand', 'start');
set_param(mdl, 'SimulationCommand', 'pause');
else
% Get model states, outputs, and inputs
% To be implemented
% Set model inputs
% To be implemented
% Step model
set_param(mdl, 'SimulationCommand', 'step');
end
end
set_param(mdl, 'SimulationCommand', 'stop')
% Get model outputs
% To be implemented
I can't figure how to access the model states, outputs or inputs data.
I have verified various model object properties, e.g., get_param(mdl, 'SaveState') and get_param(mdl, 'SaveOutput') are 'on', and the names of those variables (e.g., get_param(mdl, 'OutputSaveName') = 'yout'. But I can't figure out how to access them during runtime or when simulation is complete.
The sim() command returns the data structure containing simulation output yout and logging data. I'm trying to get that data structure, or some equivalent, using this manual stepping approach.
Antworten (1)
Paul
am 12 Feb. 2026
1 Stimme
Hi Sebastian,
5 Kommentare
Sebastian
am 12 Feb. 2026
Paul
am 13 Feb. 2026
Can you provide a link to the 2022b doc page for:
set_param(mdl, 'SimulationCommand', 'step'); ?
I couldn't find it. My recollection is that the older documentation for set_param on models was very difficult to find, and I can't find it now.
We have a nice page for it 2025b:
which does not show 'step' as valid SimulationCommand, hence my question.
Sebastian
am 13 Feb. 2026
Sebastian
am 13 Feb. 2026
Paul
am 13 Feb. 2026
On R2024a, I set up a simple simulation, had it pause at a specific time, and then used the (apparently) undocumented
set_param(gcs,SimulationCommand=step)
After the pause and after the set_param command, I have the variable 'out' in my base workspace. 'out' is the name of the variable specified in Model Settings -> Data Import/Export -> Single simulation output (with that box checked). 'out' contains the exact same information/data that you'd get from using the sim command.
Do you not see this same behavior?
Kategorien
Mehr zu Simulink finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!