setVariable - Set variable values for simulation using
SimulationInput or Simulation
object - MATLAB ([original](https://in.mathworks.com/help/simulink/slref/simulink.simulationinput.setvariable.html)) ([raw](?raw))
Set variable values for simulation usingSimulationInput
or Simulation
object
Syntax
Description
[s](#mw%5F44f50c25-6c7d-48e7-b626-f1d78464423b) = setVariable([s](#mw%5F44f50c25-6c7d-48e7-b626-f1d78464423b),[varname](#mw%5F7640fe92-ae56-46f7-8d47-d3a9e28b23e8),[varvalue](#mw%5F0813ca0a-21e3-4de4-8a1c-5c9426568e2d))
sets the value of the variable varname
to the valuevarvalue
for the simulation configured using theSimulink.SimulationInput
or Simulation
objects
. 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 aSimulationInput
or Simulation
object. Specify the value for each variable one at a time.
[s](#mw%5F44f50c25-6c7d-48e7-b626-f1d78464423b) = setVariable([s](#mw%5F44f50c25-6c7d-48e7-b626-f1d78464423b),[varname](#mw%5F7640fe92-ae56-46f7-8d47-d3a9e28b23e8),[varvalue](#mw%5F0813ca0a-21e3-4de4-8a1c-5c9426568e2d),Workspace=[mdl](#bvqk1qk-Workspace))
sets the value of the variable 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
orSimulation
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
orSimulation
object.
Examples
Specify the value for a variable using aSimulationInput
object.
Open the model.
openExample('simulink/OpenTheModelExample');
Create a SimulationInput
object for this model.
simIn = Simulink.SimulationInput('ex_sldemo_househeat');
Set the cost
variable value to50
.
simIn = setVariable(simIn,'cost',50);
By default, this variable has global workspace scope.
Simulate the model.
Modify the value of a variable in the model workspace using aSimulationInput
object.
Open the model
openExample('simulink/OpenTheModelExample');
Create a SimulationInput
object for this model.
simIn = Simulink.SimulationInput('ex_sldemo_househeat');
Set the cost
variable value to 50
and set the scope to the model workspace.
simIn = setVariable(simIn,'cost',50,'Workspace','ex_sldemo_househeat');
Simulate the model.
Input Arguments
Simulation specification or simulation in which to set variable value, 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:
- For each variable, you can specify only a single value to use for the entire simulation.
- You must specify the
SimulationInput
object as a return argument.
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:
- For each variable, you can specify the initial value to use in the simulation.
- During simulation, you can modify the variable values that define tunable parameters.
- Specifying the
Simulation
object as a return argument is optional.
Example: simin = setVariable(simin,"k",10)
sets the value of the variable k
to 10
in the simulation configuration stored on the SimulationInput
object simin
.
Example: sm = setVariable(sm,"k",10)
changes the value of the variable k
to 10
in the simulation represented by the Simulation
objectsm
.
Variable name, specified as a string or a character vector.
Example: simin = setVariable(simin,"k",10)
sets the value of the variable k
to 10
in the simulation configuration stored on the SimulationInput
object simin
.
Example: sm = setVariable(sm,"k",10)
changes the value of the variable k
to 10
in the simulation represented by the Simulation
objectsm
.
Variable value, specified as a MATLAB expression.
Example: simin = setVariable(simin,"k",10)
sets the value of the variable k
to 10
in the simulation configuration stored on the SimulationInput
object simin
.
Example: sm = setVariable(sm,"k",10)
changes the value of the variable k
to 10
in the simulation represented by the Simulation
objectsm
.
Model workspace in which to specify variable value, specified as a string or a character vector.
Example: simin = setVariable(simin,"k",10,Workspace="MyModel")
sets the value of the variable k
in the model workspace of the model named MyModel
to 10
in the simulation configuration stored on the SimulationInput
objectsimin
.
Example: sm = setVariable(sm,"k",10,Workspace="MyModel")
changes the value of the variable k
in the model workspace of the model named MyModel
to10
in simulation represented by theSimulation
object sm
.
Output Arguments
Simulation specification or simulation with specified variable value, returned as a Simulink.SimulationInput object or a Simulation object.
When you use the setVariable
function to specify the value of a variable on a SimulationInput
object, you must specify the SimulationInput
object as the return argument.
When you use the setVariable
function to specify the value of a variable on a Simulation
object, assigning the return argument is optional.
Tips
- To remove a variable from a
SimulationInput
orSimulation
object, use the removeVariable 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 the
Simulink.SimulationInput
object. Using loadVariablesFromMATFile for large number of variables is faster than usingsetVariable
.
Version History
Introduced in R2017a
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 setVariable
, getVariable
, and removeVariable
functions to configure variable values for a single simulation and tune variable values during simulation.
See Also
Objects
Functions
- loadVariablesFromMATFile | loadVariablesFromExternalSource | removeVariable | validate | sim | parsim | batchsim