Main Content

setVariable

设置模拟的变量模拟Inputobject,in

Description

example

in = in.setVariable('Name', 'Value')assigns aValueto variableName. You can add multiple variables to the model using the same模拟Inputobject.

in = in.setVariable('Name', 'Value','Workspace','ModelName')assigns theValueto variableName. Variables that are defined through the模拟Inputobject are placed in the全球工作区scope by default. The term全球工作区is specific to theSimulink.SimulationInputobject and its methods. Variables in the全球工作区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全球工作区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模拟Inputobject.

While using thesetVariablemethod, you can specify only one variable at a time.

You can usegetVariable('VariableName')method to get the value of variable and theremoveVariable('VariableName')method to remove variable from theSimulink.SimulationInputobject

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

Examples

collapse all

This example modifies the model parameters of through the模拟Inputobject.

Open the model.

Open the model.

openExample('simulink/OpenTheModelExample'); open_system('ex_sldemo_househeat'); load_system('ex_sldemo_househeat')

Create a模拟Inputobject for this model

in = Simulink.SimulationInput('ex_sldemo_househeat');

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 ofsldemo_househeatthrough the模拟Inputobject.

Set path and open the model.

openExample('simulink/OpenTheModelExample'); open_system('ex_sldemo_househeat'); load_system('ex_sldemo_househeat')

Create a模拟Inputobject for this model

in = Simulink.SimulationInput('ex_sldemo_househeat');

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

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

Simulate the model.

out = sim(in)

Input Arguments

collapse all

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValue是变量的字面值。Namemust appear inside single quotes (' ').

Example:'StartTime','1'

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

Example:'Workspace','sldemo_househeat'

Introduced in R2017a