setModelParameter - Set model parameter values for simulation using

            SimulationInput or Simulation
        object - MATLAB ([original](https://in.mathworks.com/help/simulink/slref/simulink.simulationinput.setmodelparameter.html)) ([raw](?raw))

Set model parameter values for simulation usingSimulationInput or Simulation object

Syntax

Description

s = setModelParameter([s](#mw%5Fb2b58806-d857-4679-b1f0-8e4d979f4924),[Name=Value](#namevaluepairarguments)) sets the model parameter named Name to the valueValue on the Simulink.SimulationInput or Simulation object s.

You can use the setModelParameter function to specify a value for any model parameter on a SimulationInput orSimulation object. The parameter values you specify override the values saved in the model for simulations you run using theSimulationInput or Simulation object during simulation and are reverted when the simulation completes. You can use aSimulationInput or Simulation object to override any number of parameter values.

example

Examples

collapse all

Configure model parameter values for a simulation using aSimulationInput object.

Open the model.

mdl = "sldemo_househeat"; openExample("simulink_general/sldemo_househeatExample",... SupportingFile=mdl)

Create a SimulationInput object for the model.

simin = Simulink.SimulationInput(mdl);

Specify a simulation timeout of 5 seconds, and then specify the start and stop time for the simulation.

simin = setModelParameter(simin,Timeout=5); simin = setModelParameter(simin,StartTime="1",StopTime="4");

Simulate the model.

Input Arguments

collapse all

Simulation specification or simulation in which to set model parameter, specified as a Simulink.SimulationInput object or a Simulation object.

A SimulationInput object represents a simulation specification that includes the initial state, external inputs, model parameter values, block parameter values, and variable values to use in the simulation. When you specify the first input argument as aSimulationInput object:

The Simulation object represents a simulation and provides an interface for controlling and interacting with the simulation. When you specify the first input argument as a Simulation object:

Example: simin = setModelParameter(simin,FixedStep="0.1") specifies a fixed step size of 0.1 seconds in the simulation configuration stored on theSimulationInput objectsimin.

Example: sm = setModelParameter(sm,FixedStep="0.1") changes the value of the fixed step size to 0.1 in the simulation represented by the Simulation objectsm.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: simin = setModelParameter(simin,TimeOut="100") configures a SimulationInput object with a simulation timeout of 100 seconds.

Model configuration parameter, specified as a name-value argument that consists of the programmatic name of the configuration parameter and the configuration parameter value. For example, to specify a value for the**Stop time** parameter, use the programmatic parameter nameStopTime as the name in the name-value argument and specify the simulation stop time as the value. This command sets the simulation stop time to 100 seconds on theSimulationInput object namedsimin.

simin = setModelParameter(simin,StopTime="100");

You can use the setModelParameter function to specify the value for any model configuration parameter on aSimulationInput object or aSimulation object. When you simulate a model hierarchy, the configuration parameter values you specify apply to the top model.

Example: simin = setModelParameter(simin,FixedStep="0.1") specifies a fixed step size of 0.1 seconds in the simulation configuration stored on theSimulationInput objectsimin.

Example: sm = setModelParameter(sm,FixedStep="0.1") changes the value of the fixed step size to 0.1 in the simulation represented by the Simulation objectsm.

Tips

To get a list of model configuration parameters, use thegetActiveConfigSet function and theget_param function. For example, to see the configuration parameters for a model named vdp, enter these commands in the MATLAB® Command Window.

configSet = getActiveConfigSet("vdp"); configSetNames = get_param(configSet,"ObjectParameters")

The get_param function returns a list of all the model configuration parameters, such asStopTime, SaveTime,SaveState, SaveOutput, andSignalLogging.

Since R2024a

Option to enable fast restart for individual, iterative simulations, specified as "on" or "off".

This parameter affects only individual simulations run using:

When you run multiple simulations by specifying an array ofSimulationInput objects as an input to functions such as sim, parsim, andbatchsim, enable fast restart by specifying theUseFastRestart name-value argument for thesim, parsim, orbatchsim function. For more information, seeScript Iterative or Batch Simulations Using Fast Restart.

Example: simin = setModelParameter(simin,FastRestart="on") configures theSimulationInput object simin to enable fast restart for a simulation you run by specifying the scalarSimulationInput object as an input argument to thesim function.

Example: sm = setModelParameter(sm,FastRestart="on") configures the simulation represented by the Simulation object sm to enable fast restart.

Data Types: char | string

Simulation mode, specified as "normal","accelerator", or"rapid-accelerator".

Value Description
"normal" Run simulation using normal mode.Normal mode simulations use the full model representation and provide the best support for interacting with the model during simulation.Use normal mode for workflows that involve modifying the structure of your model between simulations. For best results, use normal mode for debugging simulations.
"accelerator" Run simulation using accelerator mode.Accelerator mode simulations generate an optimized representation of the model, called a simulation target, to use in simulation. The optimizations that improve the simulation performance can reduce the ability to interact with the model during simulation. Modifying the model between accelerator mode simulations can require regenerating the simulation target.Use accelerator mode to speed up simulations in workflows that do not involve making structural changes to the model between simulations.
"rapid-accelerator" Run simulation using rapid accelerator mode.Rapid accelerator mode simulations generate a standalone executable to use for simulation. The standalone executable provides minimal support for interacting with the model during simulation. Modifying the model between rapid accelerator simulations can require rebuilding the standalone executable.Use rapid accelerator mode for the fastest simulation execution when you modify the model between simulations_only_ by tuning variable and parameter values. Tuning certain parameter values can require rebuilding the standalone executable.

For more information, see Choosing a Simulation Mode and Code Regeneration in Accelerated Models.

Example: simin = setModelParameter(simin,SimulationMode="rapid-accelerator") configures the simulation run using the SimulationInput object simin to use rapid accelerator mode.

Example: sm = setModelParameter(sm,SimulationMode="rapid-accelerator") configures the simulation represented by the Simulation object sm to simulate using rapid accelerator mode.

Tips

Data Types: char | string

Since R2024a

Option to capture simulation errors in simulation output, specified as"off" or "on". This parameter affects only individual simulations run using:

When you run multiple simulations by specifying an array ofSimulationInput objects as an input to functions such as sim, parsim, andbatchsim, the CaptureErrors parameter is always enabled.

Behavior CaptureErrors="off" (default) CaptureErrors="on"
Issuing exceptions for simulation errors The software issues exceptions for simulation errors.The exception stops both the simulation in which the error occurred and the function or script that invoked the simulation, if applicable. The software does not issue exceptions for simulation errors.The simulation error stops the simulation. However, if a script or another function invoked the simulation, the simulation error does not interrupt the function or script execution.
Error reporting Errors that occur during simulation are reported in the MATLAB Command Window. Information about the simulation errors, including the message and the simulation phase in which the error occurred, is captured in the simulation output.
Simulation results Issuing an exception stops the simulation immediately. The sim function does not return simulation results. The sim function returns simulation results and metadata up to the simulation time at which the error occurred.

Example: simin = setModelParameter(simin,CaptureErrors="on") configures a single simulation run using the scalar SimulationInput object simin to capture simulation errors in the simulation output.

Example: sm = setModelParameter(sm,CaptureErrors="on") configures the simulation represented by the Simulation objectsm to capture simulation errors in the simulation output.

Tips

Data Types: char | string

Option to disable rebuilding rapid accelerator target, specified as"on" or "off". When you specify this argument as "on", changes that require rebuilding the rapid accelerator target are ignored. When you use this option, modify only options that do not require rebuilding the rapid accelerator target.

Example: simin = setModelParameter(simin,RapidAcceleratorUpToDateCheck="off") configures a SimulationInput object to disable rebuilding the rapid accelerator target.

Example: sm = setModelParameter(sm,RapidAcceleratorUpToDateCheck="off") disables the rapid accelerator up-to-date check in the simulation represented by the Simulation objectsm.

Data Types: char | string

Maximum simulation run time, specified as a positive scalar. Specify the time, in seconds, to allow the simulation to run. If the simulation runs for longer than the value you specify, the software issues a warning and stops the simulation. For example, if you specifyTimeOut as 30, the software stops the simulation and issues a warning if computing simulation results takes more than 30 seconds.

The TimeOut parameter specifies a limit on the amount of clock time for a simulation to run. To specify the maximum time value to simulate, use the Stop time parameter.

Example: simin = setModelParameter(simin,TimeOut=60) sets a maximum run time of 60 seconds in the simulation configuration stored on the SimulationInput objectsimin.

Example: sm = setModelParameter(sm,TimeOut=60) sets a maximum run time of 60 seconds in the simulation represented by theSimulation objectsm.

Data Types: char | string

Output Arguments

collapse all

Simulation specification or simulation with model parameter added or modified, returned as a Simulink.SimulationInput object or a Simulation object.

When you use the setModelParameter function to specify the value of a model parameter on a SimulationInput object, you must specify the SimulationInput object as the return argument.

When you use the setModelParameter function to specify the value of a model parameter on a Simulation object, assigning the return argument is optional.

Tips

Version History

Introduced in R2017a

expand all

The Simulation object represents a simulation and provides an interface to control simulation execution and tune model parameter, block parameter, and variable values during simulation. You can use the setModelParameter function to specify model parameter values to use during simulation and to modify values of tunable model parameters during simulation.

You can use the setModelParameter function to enable theFastRestart parameter on a SimlationInput object or a Simulation object. Fast restart saves time in iterative simulation workflows by skipping compilation and termination after compiling the model for the first simulation you run after enabling fast restart.

The FastRestart parameter applies only for individual simulations you run one at a time using the Simulation object or the sim function. To use fast restart when you run multiple simulations using an array of SimulationInput objects and thesim, parsim, orbatchsim functions, specify theUseFastRestart name-value argument for thesim, parsim, orbatchsim function.

For more information, see How Fast Restart Improves Iterative Simulations.

In prior releases, the sim function overwrote theCaptureErrors parameter for a single simulation if you specified one or more name-value arguments in addition to the scalarSimulationInput object, including theStopOnError name-value argument. Now, thesim function has consistent default behavior and options, summarized in the table.

SimulationInput Dimensions CaptureErrors StopOnError Behavior Change
scalar "off" by default.To enable this parameter, specify the parameter on theSimulationInput object using thesetModelParameter function. This name-value argument has no effect when you run a single simulation. Some calls to the sim function in scripts or functions from previous releases might not return aSimulink.SimulationOutput object when the same calls had returned the simulation output in previous releases. To restore the prior behavior, specify theCaptureErrors parameter as"on".
vector, matrix, array Always "on". "off" by default.Specify this name-value argument as "on" to skip subsequent simulations if a simulation error occurs. No change.