Main Content

setVariable

Set variable value on SimulationInput object

Description

example

simIn = setVariable(simIn,varName,varValue) sets the variable varName with the value varValue on the Simulink.SimulationInput object simIn. You can configure a Simulink.SimulationInput object with multiple variables. When you use the setVariable function, specify only one variable at a time.

simIn = setVariable(simIn,varName,varValue,'Workspace',wkspace) assigns the value varValue to variable varName. Variables that are defined through the SimulationInput object are placed in the global workspace scope by default. The term global workspace is specific to the Simulink.SimulationInput object and its functions. Variables in the global workspace scope take precedence if a variable with the same name exists in the base workspace or the data dictionary. The variables in the model workspace take precedence over the global workspace scope. To change the value of a model workspace variable, set the scope by specifying the model name when you add the variable to the SimulationInput object.

You can use getVariable(simIn,varName) to get a variable value and removeVariable(simIn,varName) to remove a variable from the Simulink.SimulationInput object

For information on using nonscalar variables, structure variables, and parameter objects, see Sweep Nonscalars, Structures, and Parameter Objects.

Examples

collapse all

Specify the value for a variable using a Simulink.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 Simulink.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 inputs and configuration, specified as a Simulink.SimulationInput object.

Variable name, specified as a string or a character vector.

Variable value, specified as a MATLAB expression.

Variable scope, specified as the model name.

Example: 'Workspace','sldemo_househeat'

Output Arguments

collapse all

Simulation configuration with variable added, returned as a Simulink.SimulationInput object.

Version History

Introduced in R2017a