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.

You can specify values for multiple variables on aSimulationInput or Simulation object. Specify the value for each variable one at a time.

example

[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.

example

Examples

collapse all

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

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 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 = 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

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

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 setVariable, getVariable, and removeVariable functions to configure variable values for a single simulation and tune variable values during simulation.

See Also

Objects

Functions

Topics