setVariable
Set variable values for simulation using
SimulationInput
or Simulation
object
Description
sets the value of the variable s
= setVariable(s
,varname
,varvalue
)varname
to the value
varvalue
for the simulation configured using the
Simulink.SimulationInput
or Simulation
object
s
. Use this syntax to specify values for variables in the
base workspace or data dictionaries. The variable values you specify override the
variable values saved in the base workspace or data dictionary during simulation and
are reverted when the simulation completes.
When
s
is aSimulationInput
object, you can specify only a single value for each variable to use for the entire simulation.When
s
is aSimulation
object, you can specify a single value for each variable to use at the start of the simulation and specify new values during simulation for variables that define tunable values.
You can specify values for multiple variables on a
SimulationInput
or Simulation
object. Specify
the value for each variable one at a time.
sets the value of the variable s
= setVariable(s
,varname
,varvalue
,Workspace=mdl
)varname
in the model workspace
of the model mdl
.
By default, when you do not specify the Workspace
argument,
variables on a SimulationInput
or Simulation
object are scoped to a global workspace specific to each object. Variables in the
global workspace of a SimulationInput
or
Simulation
object override variables with the same name in the
base workspace and in data dictionaries but not in the model workspace.
Use this syntax to specify the value of a variable in a model workspace for a
simulation configured using a SimulationInput
or
Simulation
object.
Examples
Input Arguments
Output Arguments
Tips
To remove a variable from a
SimulationInput
orSimulation
object, use theremoveVariable
function.To get the value set on a
SimulationInput
orSimulation
object for a specific variable, use thegetVariable
function. For example, this command gets the value of the variable namedMyVariable
.varvalue = getVariable(s,"MyVariable");
To run multiple simulations with large number of variables (for example, over a hundred variables), use the
loadVariablesFromMATFile
function to load variables into theSimulink.SimulationInput
object. UsingloadVariablesFromMATFile
for large number of variables is faster than usingsetVariable
.