Accessing Simulink model inputs and outputs when stepping model via MATLAB set_param(mdl, 'SimulationCommand', 'step') command

29 views (last 30 days)
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.

Answers (1)

Paul
Paul on 12 Feb 2026 at 2:32
Hi Sebastian,
Check out the doc page for using a simulation object. It make it easier to do what you want.
  5 Comments
Paul
Paul on 13 Feb 2026 at 21:08
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?

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!