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

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)

Hi Sebastian,
Check out the doc page for using a simulation object. It make it easier to do what you want.

5 Comments

Thank you, Paul. The simulaiton object looks like it integrates all the functionality and data elements I need. Unfortunately, it seems it was released in 2024a. I am required to use 2022b, which does not seem to have it.
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.
Unfortunately, I cannot remember where I found I found set_param(mdl, 'SimulationCommand', 'step') documented, and I am not able to find it again. However, I have been using it, and the simulation executes to completion by calling it repeatedly.
Thank you for the link to the Run Simulations Programmatically for 2025b. A nice description.
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 Simulink in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 12 Feb 2026

Commented:

on 13 Feb 2026

Community Treasure Hunt

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

Start Hunting!