Main Content

setVariable

Set variable values for simulation using SimulationInput or Simulation object

Description

example

s = setVariable(s,varname,varvalue) sets the value of the variable 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 a SimulationInput object, you can specify only a single value for each variable to use for the entire simulation.

  • When s is a Simulation 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.

example

s = setVariable(s,varname,varvalue,Workspace=mdl) 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 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

collapse all

Specify the value for a variable using a SimulationInput 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.

simIn = setVariable(simIn,'cost',50);

By default, this variable has global workspace scope.

Simulate the model.

out = sim(simIn);

Modify the value of a variable in the model workspace using a SimulationInput 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.

out = sim(simIn);

Input Arguments

collapse all

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 a SimulationInput 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 object sm.

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 object sm.

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 object sm.

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 object simin.

Example: sm = setVariable(sm,"k",10,Workspace="MyModel") changes the value of the variable k in the model workspace of the model named MyModel to 10 in simulation represented by the Simulation object sm.

Output Arguments

collapse all

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 or Simulation object, use the removeVariable function.

  • To get the value set on a SimulationInput or Simulation object for a specific variable, use the getVariable function. For example, this command gets the value of the variable named MyVariable.

    varvalue = getVariable(s,"MyVariable");

Version History

Introduced in R2017a

expand all