Main Content

Simulink.VariantVariable class

Package:金宝app

Create a variant parameter object

Description

TheSimulink.VariantVariableclass creates a variant parameter object. You can specify multiple values for each object. You can also specify properties such as dimension, data type, and so on. Each value of the object is associated with a variant condition expression. Set the parameter values and properties in the object, then reference the object from the block.

Before you create a newSimulink.VariantVariableobject, create aSimulink.VariantControlobject representing the variant control variable to be used in theSimulink.VariantVariableobject.

Note

You can create the variant parameter object only in a base or in a data dictionary.

Creation

P= Simulink.VariantVariablecreates an empty variant parameter object.

P= Simulink.VariantVariable(Name,Value)creates a variant parameter object as specified by one or moreName,Valuepair arguments. Using this syntax, you can specify multiple values for the object and associate each value with a variant condition expression. You can also specify other properties of the object such as data types, storage class, dimensions, and so on.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:'Specification', 'Pspec', 'Choices', {'V==1', 4.5, 'V==2', 9.5}

Properties of variant parameter object such as data type, dimensions, storage class, and so on, specified as theSimulink.Parameterobject. The default storage class of aSimulink.Parameterobject isAuto. If you specify the storage class asAuto, the value of the object is inline to the literal value in the generated code. If you specify the storage class of the object anything other thanAuto对象表示为可调参数rs using symbolic names in the generated code. For more information, seeChoose Storage Class for Controlling Data Representation in Generated Code(Simulink Coder).

Note

If the variant parameter object is associated to a variant control object having astartupactivation time then specifying the storage class of the variant parameter object to any of these is not supported:

  • Storage classes such asDefinethat generates macros in the code.

  • Storage classes such asConstthat generates constant qualified variables in the code.

Example:'Specification', 'Pspec'

Variant conditions and values, specified as a struct. During simulation, when a variant condition evaluates totrue, its associated value becomes active. When a variant condition evaluates tofalse, its associated value becomes inactive. No two values of the same variant parameter must be associated with the same variant condition.

You can specify the variant condition as boolean MATLAB®expressions that contain one or more operands and operators. SeeVariant Controls in Variant Parameters.

Example:'Choices', {'V==1', 4.5, 'V==2', 9.5}

Methods

expand all

Examples

collapse all

Scalar MATLAB® variables allow you to rapidly prototype variant values when you are still building your model. They help you focus more on building your variant values than on developing the expressions that activate those choices.

You can define scalar control variables in locations listed inStorage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.

In the MATLAB Editor, specify variant choices in their simplest form as scalar variables in aSimulink.VariantVariableobject.

vpObj = Simulink.VariantVariable('Choices',{'VCtrl==1', 1000,'VCtrl==2', 2000})
vpObj = VariantVariable with 2 choices: Condition Value __________ _____ VCtrl == 1 1000 VCtrl == 2 2000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Activate one of the variant values by defining a control variable,VCtrl, and setting its value to1in aSimulink.VariantControlobject. This condition activates the value1000and assigns this value tovpObj.

VCtrl = Simulink.VariantControl('Value', 1,'ActivationTime','update diagram')
VCtrl = VariantControl with properties: Value: 1 ActivationTime: 'update diagram'

If you change the value ofVCtrlto2, Simulink® sets the value ofvpObjto2000.

VCtrl.Value = 2;

If you intend to generate code for a model containing variant parameter values, specify variant control variables as MATLAB® variables orSimulink.Parameterobjects.Simulink.Parameterobjects allow you to specify other attributes (such as data type) that are required for generating code.

Note:

  • You can define a variant control variable of typeSimulink.Parameteronly in the base workspace or in a data dictionary. DefiningSimulink.Parametertype of variant control variables in the mask or model workspace is not supported. For more information on storage locations for variant control variables, seeStorage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.

  • Simulink.Parameterobjects within structures and that have data types other thanSimulink.Busobjects are not supported.

In the MATLAB Editor, define aSimulink.Parameterobject.

VSSMODE = Simulink.Parameter; VSSMODE.Value = 1; VSSMODE.DataType ='int32'; VSSMODE.CoderInfo.StorageClass ='Custom'; VSSMODE.CoderInfo.CustomStorageClass ='ImportedDefine'; VSSMODE.CoderInfo.CustomAttributes.HeaderFile ='rtwdemo_importedmacros.h';

Variant control variables defined asSimulink.Parameterobjects can have one of these storage classes:

  • Define or ImportedDefinewith header file specified

  • CompilerFlag

  • SystemConstant(AUTOSAR)

  • Your own storage class that defines data as a macro

You can also convert a scalar variant control variable into aSimulink.Parameterobject. For more information, seeConvert Variant Control Variables into Simulink.Parameter Objects.

Specify the object as a variant control using aSimulink.VariantVariableobject.

vpObj = Simulink.VariantVariable('Choices', {'VCtrl == 1',1000,'VCtrl == 2',2000});

Activate one of the variant values by defining a control variableVCtrland setting its value toVSSMODEin aSimulink.VariantControlobject. This condition activates the value1000and assigns this value tovpObj.

VCtrl = Simulink.VariantControl('Value',VSSMODE,'ActivationTime','code compile')
VCtrl = VariantControl with properties: Value: [1x1 Simulink.Parameter] ActivationTime: 'code compile'

After identifying the variant values that your model requires, you can construct complex variant conditions to control the activation of your variant parameter values by defining variant conditions asSimulink.Variantobjects.Simulink.Variantobjects enable you to reuse common variant conditions across models and help you encapsulate complex variant condition expressions.

Note:

In the MATLAB® Editor, encapsulate variant control expressions asSimulink.Variantobjects.

LinearController=Simulink.Variant('VCtrl==1'); NonLinearController=Simulink.Variant('VCtrl==2');

Specify theSimulink.Variantobjects as the variant controls in aSimulink.VariantVariableobject.

vpObj = Simulink.VariantVariable('Choices',{'LinearController',1000,'NonLinearController',2000})
vpObj = VariantVariable with 2 choices: Condition Value ___________________ _____ LinearController 1000 NonLinearController 2000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Activate one of the variant values by defining a control variable,VCtrl, and setting its value toLinearControllerin aSimulink.VariantControlobject. This condition activates the value1000and assigns this value tovpObj.

VCtrl = Simulink.VariantControl('Value',1,'ActivationTime','update diagram')
VCtrl = VariantControl with properties: Value: 1 ActivationTime: 'update diagram'

Using this approach, you can develop complex variant condition expressions that are reusable.

Introduced in R2021a