sim
Simulate Simulink model
Syntax
Description
Simulink.SimulationInput
Object Syntax
runs one or more simulations of a Simulink® model according to the properties defined on one or more simOut
= sim(simIn
)Simulink.SimulationInput
objects.
If
simIn
is a scalarSimulink.SimulationInput
object, thensimOut
is a scalarSimulink.SimulationOutput
object.If
simIn
is an array ofSimulink.SimulationInput
objects, thensimOut
is an array ofSimulink.SimulationOutput
objects.
You can use a SimulationInput
object to configure options and
inputs for simulations, including:
The model to simulate
Source variables or files for external input data
Block parameter values to use for the simulation
Model configuration parameter values to use for the simulation
When a property of the SimulationInput
object modifies a
model or block parameter value, the value is modified during simulation and
reverted at the end of the simulation.
When you configure programmatic simulations using
SimulationInput
objects, you can easily transition from
using the sim
function to using other functions, such as
parsim
and batchsim
.
simulates a model according to the properties defined on the
simOut
= sim(simIn
,Name,Value
)Simulink.SimulationInput
object simIn
with additional options specified using one or more name-value arguments.
For a list of name-value arguments supported for the
Simulink.SimulationInput
syntax, see Simulink.SimulationInput Object Syntax.
Model Name Syntax
simulates the model specified by simOut
= sim(modelName
)modelName
using the current
configuration parameter and block parameter values for the model.
If the model has the Single simulation output parameter enabled,
simOut
is aSimulink.SimulationOutput
object.If the model does not have the Single simulation output parameter enabled,
simOut
is a vector that contains the simulation times. For more information, see Returning multiple output arguments not recommended.
simulates the model specified by simOut
=
sim(modelName
,Name,Value
)modelName
with options
specified using one or more name-value arguments. For example, you can modify a
model configuration parameter value for the simulation by specifying the
parameter name and value as a name-value argument.
When you modify model configuration parameters by providing inputs to the
sim
function, the changes are applied during simulation
and reverted at the end of the simulation.
For a list of name-value arguments supported for the model name syntax, see Model Name Syntax.
simulates the model specified by simOut
=
sim(modelName
,paramStruct
)modelName
using the model
configuration parameter values specified by the structure
paramStruct
.
Examples
Input Arguments
Output Arguments
Tips
To ensure the
sim
function returns results in the same format regardless of which input arguments you specify, save your model with the Single simulation output parameter enabled. With this option enabled, simulation results are returned as aSimulink.SimulationOutput
that contains all logged data as well as simulation metadata, including timing information and diagnostics. Analyzing results from multiple simulations is easier when all simulation data and metadata is stored in a single object.To get a list of model configuration parameters, use the
getActiveConfigSet
function and theget_param
function. For example, to see the configuration parameters for the modelvdp
, enter these commands in the MATLAB Command Window.configSet = getActiveConfigSet("vdp"); configSetNames = get_param(configSet,"ObjectParameters")
The return from the
get_param
function lists the model configuration parameters such asStopTime
,SaveTime
,SaveState
,SaveOutput
, andSignalLogging
.When you simulate a model hierarchy, model configuration parameters you specify as input arguments to the
sim
function apply to the top model.When you run a simulation using the
sim
function, the simulation runs until an error occurs or the simulation reaches the specified stop time. To programmatically run an interactive simulation that you can pause and continue programmatically, use theset_param
function with theSimulationCommand
input argument. For more information, see Run Simulations Programmatically.When you simulate a model with infinite stop time, stop the simulation from the MATLAB Command Window by pressing Ctrl+C. The simulation stops, and simulation results are not saved in the MATLAB workspace.
Configure logging for time, states, and outputs using the Configuration Parameters dialog box. On the Modeling tab, under Setup, click Model Settings. Then, in the Configuration Parameters dialog box, select Data Import/Export.
To log signals throughout a model, use signal logging or logging blocks such as the To Workspace block or the Record, XY Graph block. For more information about signal logging, see Save Signal Data Using Signal Logging.