Documentation

setVariable

Set variables for a simulation through SimulationInput object, in

Syntax

in = in.setVariable(Name,Value)
in = in.setVariable(Name,Value,'Workspace', 'ModelName')

Description

example

in = in.setVariable(Name,Value) assigns a Value to variable Name. You can add multiple variables to the model using the same SimulationInput object.

in = in.setVariable(Name,Value,'Workspace', 'ModelName') assigns the Value to variable Name. 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 methods. 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.

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

Examples

collapse all

This example modifies the model parameters of through the SimulationInput object.

Open the model.

mdl = 'sldemo_househeat';
open_system(mdl);

Create a SimulationInput object for this model

in = Simulink.SimulationInput(mdl);

Set the variable value to 50.

in = in.setVariable('cost',50);

By default, this variable is placed in the global workspace scope.

Simulate the model.

out = sim(in)

This example modifies the model parameters of through the SimulationInput object.

Set path and open the model.

mdl = 'sldemo_househeat';
open_system(mdl);

Create a SimulationInput object for this model

in = Simulink.SimulationInput(mdl);

Set the variable to 50 and set the scope to model workspace.

in = in.setVariable('cost',50,'Workspace','sldemo_househeat');

Simulate the model.

out = sim(in)

Input Arguments

collapse all

Name-Value Pair Arguments

Example: 'cost','65'

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the literal value of the variable. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

collapse all

Set the scope of the defined variable by specifying the model name

Example: 'Workspace', 'sldemo_househeat'

Introduced in R2017a

Was this topic helpful?