Control Simulation Using the set_param Command

How Using set_param to Control Simulation Works

You can use the set_param command to

  • Start a simulation,

  • pause, stop or continue a simulation

  • Update a block diagram.

  • Write all data logging variables to the base workspace.

Keep the following in mind when using set_param to control a simulation:

  • When you use set_param to start, pause, continue or stop a simulation, these commands are requests for such actions and do not get executed immediately. Simulink® first completes uninterruptable work, such as solver steps and other commands that preceded the set_param command in the script the set_param command is a part of. Then simulation starts, pauses, continues or stops as specified by the set_param command.

  • If you use matlab -nodisplay to start a session, you cannot use set_param to run your simulation. The -nodisplay mode does not support menu simulation.

  • When you start a simulation using the set_param command and 'start' argument, you must use the set_param command and 'stop' argument to stop it.

  • You can also use the sim command to start a simulation. However, you cannot pause a simulation started with sim.

set_param Syntax

Use this syntax to control simulation using the set_param command:

  • 'sys' is the name of the system you want to simulate

  • 'cmd' is the command to control simulation. Possible values are:

    • 'start'

    • 'stop'

    • 'pause'

    • 'continue'

    • 'connect' (to a target)

    • 'update'

    • 'WriteDataLogs'

For example, to start a simulation on the system vdp, use the following command:

set_param('vdp', 'SimulationCommand', 'start')

Update Workspace Variables Dynamically During Simulation

You can update work space variables dynamically when a simulation is running. To do this, type:


Check Status of Simulation

You can also use the get_param command to check the status of a simulation. The format of the get_param function for this use is


The software returns 'stopped', 'initializing', 'running', 'paused', 'updating', 'terminating', or 'external' (used with the Simulink Coder™ product).

Control Simulation Using Block Callbacks

A callback executes when you perform various actions on your model, such as clicking on a block or starting a simulation. You can use callbacks to execute a MATLAB® script or other MATLAB commands.

Provide code for the appropriate block callback parameter to specify actions that occur at one of these times:

  • At the start of model simulation

  • After the simulation pauses

  • When the simulation continues

  • When the simulation stops

For details, see Callbacks for Customized Model Behavior and Block Callback Parameters.

Execute MATLAB Code Before Starting Simulation

You can use the StartFcn callback to automatically execute MATLAB code before the simulation starts.

% openscopes.m 
% Brings scopes to forefront at beginning of simulation.

blocks = find_system(bdroot,'BlockType','Scope');

% Finds all of the scope blocks on the top level of your
	% model to find scopes in subsystems, give the subsystem
	% names. Type help find_system for more on this command.

for i = 1:length(blocks)

% Loops through all of the scope blocks and brings them
	% to the forefront

After you create this MATLAB script, set the StartFcn parameter for the model to call the script. For example,


Now every time you run the model, all of the Scope blocks automatically open in the forefront.

Related Examples

Was this topic helpful?