Simulink.VariantVariable - Create variant parameter object - MATLAB (original) (raw)

Namespace: Simulink

Create variant parameter object

Since R2021a

Description

Use the Simulink.VariantVariable class to create a variant parameter object. Variant parameter objects enable you to vary the values of block parameters in a Simulink® model conditionally. For an overview of variant parameters, see Use Variant Parameters to Reuse Block Parameters with Different Values.

You can specify multiple values for a variant parameter object and also set properties such as data type and dimensions applicable to all the choice values. Each value of the variant parameter object is associated with a variant condition expression. After creating the object, use it to set the value of block parameters in a model, such as theGain parameter of a Gain block. During simulation, the value associated with the variant condition that evaluates to true becomes the active value for that parameter. The values associated with the conditions that evaluate to false become inactive.

Before you create a new Simulink.VariantVariable object, create a Simulink.VariantControl object representing the variant control variable to be used in the Simulink.VariantVariable object. Use these variant control variables to specify the variant condition expression associated with each choice value of a variant parameter, for example, 'V == 1' where V is aSimulink.VariantControl object. The Simulink.VariantControl object allows you to specify an ActivationTime for the variant parameter and a Value that is used to evaluate the variant conditions and determine the active choice of the variant parameter. For examples on usage of each type of variant control value, see Use Variant Control Variables in Variant Parameters.

Note

Creation

`P` = Simulink.VariantVariable creates an empty variant parameter object.

`P` = Simulink.VariantVariable([PropertyName=Value](#mw%5F80f41b66-a9d0-4926-98db-78b6b82692e0)) creates a variant parameter object and sets Properties using one or more name-value arguments. Use this syntax to specify multiple values for the object and to associate each value with a variant condition expression. You can also specify other properties of the object such as data type, storage class, and dimension.

Properties

expand all

Variant conditions and values, specified as a cell array. During simulation, when a variant condition evaluates to true, its associated value becomes active. When a variant condition evaluates to false, its associated value becomes inactive. No two values of the same variant parameter can be associated with the same variant condition.

You can specify the variant conditions as boolean MATLAB® expressions. For information on the supported operators and operands, seeVariant Control Mode in Variant Parameters.

The value of a choice of a variant parameter can be one of these types:

Arithmetic and logical operations on a variant parameter object applies the operation on the active choice value. For example, `p` + 2 adds 2 to the active choice value ofp.

You can specify one of the choice values as a default value for the variant parameter by setting its variant condition to the (default) keyword. Simulink uses the default value for the variant parameter when none of the other variant conditions evaluate to true.

Example: P = Simulink.VariantVariable('Choices',{'V == 1',4.5,'V == 2',9.5})

Example: P = Simulink.VariantVariable('Choices',{'V == 1',3,'(default)',6})

Example: slparam_1 = Simulink.Parameter(slexpr("a_vp+b_vp")); slparam_2 = Simulink.Parameter(slexpr("a_vp-b_vp")); vp_tunable = Simulink.VariantVariable('Choices', {'vc_startup==1','slparam_1','vc_startup==2','slparam_2'})

Attributes:

GetAccess private
SetAccess private

Properties of variant parameter object such as data type, dimensions, and storage class applicable to all the choice values, specified as aSimulink.Parameter object. You can override the specification when setting a choice value explicitly.

The default storage class of a Simulink.Parameter object isAuto. If you specify the storage class as Auto, the value of the object is inlined to the literal value in the generated code. If you specify the storage class of the object anything other than Auto, the object is represented as tunable parameters using symbolic names in the generated code. For more information, see Options to Represent Variant Parameters in Generated Code (Embedded Coder).

Note

When using both Specification and Bank properties for a Simulink.VariantVariable object, the specification object must have storage class set to Auto and it must not use symbolic dimensions. For a variant parameter bank, use a Simulink.VariantBankCoderInfo object to specify code generation attributes.

Example: P = Simulink.VariantVariable('Specification','sp','Choices',{'V == 1',2,'V == 2',3});

Attributes:

GetAccess public
SetAccess public

Name of variant parameter bank (Simulink.VariantBank) object, specified as a string scalar or character vector.

With Embedded Coder®, you can group variant parameters in the generated code by adding them to the same variant parameter bank. The variant parameters must have activation time set tostartup and must have the same set of variant conditions. Use the Simulink.VariantBank class to create a variant parameter bank object and set the Bank property of the Simulink.VariantVariable object to the name of the variant parameter bank. For more information, see Simulink.VariantBank.

Example: P = Simulink.VariantVariable('Bank','ParamBank','Choices',{'V == 1',2,'V == 2',3});

Attributes:

GetAccess public
SetAccess public

Data Types: string | char

Methods

expand all

addChoice Add choices to variant parameter object
getChoice Return choices of variant parameter object
setChoice Modify existing choices of variant parameter object
removeChoice Remove choices from variant parameter object

These methods specialize standard MATLAB operators and functions for objects in this class.

bitcmp Performs bit-wise complement on input.
eq Determines whether the inputs are equal.
gt Determines whether the first input is greater than the second input.
mrdivide Divides the first input by the second input.
or Performs a logical OR on inputs.
uminus Negates the input.
bitor Performs bit-wise OR on inputs.
ge Determines whether the first input is greater than or equal to the second input.
le Determines whether the first input is less than or equal to the second input.
mtimes Multiplies the inputs.
plus Adds the inputs.
uplus Returns the value of the input.
and Performs logical AND on inputs.
bitshift Shift bits by specified number of places.
lt Determines whether the first input is less than the second input.
ne Determines whether the inputs are not equal.
bitand Performs bit-wise AND on inputs.
bitxor Performs bit-wise XOR on inputs.
minus Subtracts the second input from the first input.
not Returns logical NOT of input.

Examples

collapse all

This example shows you how to create variant parameters using the Simulink.VariantVariable class in the base workspace. To create the variant control and variant parameter objects in a data dictionary, see Add Entry to Design Data Section of Data Dictionary. To create the objects in model workspace, see Change Model Workspace Data.

Create Variant Parameter

In the MATLAB® Command Window, define a variant control variable V using the Simulink.VariantControl class and set its value to 1. The default variant activation time for the variant control is update diagram.

V = Simulink.VariantControl('Value',1);

Create a variant parameter object by specifying the variant conditions and values for the choices.

Use V to specify a variant condition for each choice of the variant parameter. During simulation, the choice value that corresponds to the variant condition that evaluates to true becomes the active value of the parameter. Thus, the variant control V allows you to set one of the choice values as the active variant.

Since the value of V is 1, the condition V == 1 evaluates to true when you simulate the model and hence K1 is assigned a value of 3.5.

K1 = Simulink.VariantVariable('Choices',{'V == 1',3.5,'V == 2',8.3});

You can perform arithmetic and logical operations on the variant parameter object.

Specify Simulink.Parameter Object as Variant Parameter Value

You can assign a Simulink.Parameter object's name as the value of a variant parameter object, enabling you to define code generation settings for each variant parameter value. These Simulink.Parameter objects must be defined in the same workspace as the variant parameter. Additionally, all Simulink.Parameter objects used with a single variant parameter must have the same datatype, complexity, dimensions, and storage class attributes.

V = Simulink.VariantControl('Value',1); spValue1 = Simulink.Parameter(3.5); spValue1.CoderInfo.StorageClass = 'ImportedExtern'; spValue2 = Simulink.Parameter(8.3); spValue2.CoderInfo.StorageClass = 'ImportedExtern'; K1 = Simulink.VariantVariable('Choices',{'V == 1','spValue1','V == 2','spValue2'});

Specify Default Value for Variant Parameter

To specify one of the choice values as a default value for the parameter, use the (default) keyword for the variant condition. Simulink® uses the default value for the variant parameter when none of the other variant conditions evaluate to true. When activation time is set to code compile, this default value is assigned in the #else condition. When activation time is set to startup, the value is assigned in the else condition in the generated code.

K2 = Simulink.VariantVariable('Choices',{'V == 1',3,'(default)',6});

Analyze Variant Parameter Values for Consistent Signal Attributes

When you specify a variant activation time other than update diagram for the Simulink.VariantControl, Simulink analyzes all choice values of the variant parameters for consistency of signal attributes such as datatype, dimensions, and complexity. Activation time also determines whether the generated code must contain only the active choice or both active and inactive choices.

Create a variant control variable V with variant activation time set to update diagram analyze all choices.

V = Simulink.VariantControl('Value',1,'ActivationTime','update diagram analyze all choices'); K4 = Simulink.VariantVariable('Choices',{'V == 1',int8(3),'V == 2',int8(7)});

If you add the choice value 5.1 to K4 and use K4 in a model, the model compilation fails because the data type of the choice is not of type int8.

K4 = setChoice(K4,{'V == 3',5.1});

Specify Common Code Generation Properties for Variant Parameter Values

If you intend to generate code for a model containing variant parameters, use a Simulink.Parameter object to define specifications such as data type, dimensions, and storage class for all the variant choices.

sp = Simulink.Parameter(int8([])); sp.CoderInfo.StorageClass = 'Custom'; sp.CoderInfo.CustomStorageClass = 'Define'; sp.CoderInfo.CustomAttributes.HeaderFile = 'vparam.h';

Define a variant control variable V using the Simulink.VariantControl class.

V = Simulink.VariantControl('Value',1);

Create a variant parameter and set the Specification property. This command defines all the choices to be of type int8 with the specified custom storage class attributes for code generation.

K3 = Simulink.VariantVariable('Specification','sp','Choices',{'V == 1',4,'V == 2',8});

The example shows you how to create a variant parameter with choice values of different dimensions. All other attributes, such as data type and complexity, must be the same across all choices. To generate code for variant parameters that have values with different dimensions, use Embedded Coder®. If you set the activation time of the associated Simulink.VariantControl object to code compile, Embedded Coder generates code for both active and inactive values of variant parameters.

In this example, the variant parameter kv1 has values with different dimensions and the variant activation time of the associated variant control variable object V is set to code compile. The storage class of kv1 is set to exported data scope (ExportedGlobal (Embedded Coder)) to generate code that reuses the variant parameter values that you specify. You can choose to generate code that imports variant parameter values and its dimensions from external code prior to code compilation by setting the storage class with imported data scope (ImportedExtern, ImportedExternPointer (Embedded Coder)).

% Variant parameter specification spec = Simulink.Parameter(); spec.CoderInfo.StorageClass = 'ExportedGlobal';

% variant control variable V = Simulink.VariantControl('Value',1,'ActivationTime','code compile');

% variant parameter choice1 = 'V == 1'; choice2 = 'V == 2'; choice3 = 'V == 3'; kv1 = Simulink.VariantVariable('Choices',... {choice1,[1, 2, 3, 4],choice2,[1, 2, 3, 4, 5],choice3,[1, 2, 3, 4, 5, 6]},... 'Specification','spec');

When you generate code for a model that uses kv1, the code contains active and inactive values of kv1 with dimensions represented as symbols. All values are enclosed in C preprocessor conditionals #if and #elif. When you compile the code, Simulink® evaluates the preprocessor conditionals and preserves the code only for the active value of the variant parameter. For more information, see Compile Code Conditionally for All Values of Variant Parameters with Same and Different Dimensions (Embedded Coder).

Set the value of a Simulink.VariantControl object and the value of a choice of a Simulink.VariantVariable object to mathematical expressions involving literal numbers, MATLAB® variables, or Simulink.Parameter objects. With expressions, you can:

To specify a mathematical expression for the value of a Simulink.VariantControl object or the value of a choice of a Simulink.VariantVariable object:

  1. Create a Simulink.Parameter object or an object of a class that inherits from Simulink.Parameter.
  2. Set the Value property of the object to the expression by using the slexpr function.
  3. Use the object to set the value of the variant control or the values of the choices of a variant parameter.

You can generate C or C++ code that preserves the expressions using Embedded Coder® by applying compatible storage classes. For applicable code generation techniques and considerations, see Code Generation of Parameter Objects With Expression Values (Embedded Coder).

Explore Example

The example model uses a variant parameter object vp_tunable that is used to set the Gain parameter of a Gain block. The model configuration parameter Default parameter behavior for code generation optimization is set to Tunable.

var-param-slexpr-model.png

vp_tunable uses a Simulink.VariantControl object vc_startup that has activation time set to startup and ExportedGlobal storage class, which yields a global variable in the generated code. The choices of vp_tunable are Simulink.Parameter objects with values set to mathematical expressions. The values of the parameter objects a_vp, b_vp, a_vc, and b_vc that are used in the expressions are imported from an external file external_imp_defines.h.

% These parameter objects have the CustomStorageClass property set to ImportedDefine and HeaderFile property set to 'external_imp_defines.h' a_vc = Simulink.Parameter(int32(1)); b_vc = Simulink.Parameter(int32(2)); a_vp = Simulink.Parameter(int32(6)); b_vp = Simulink.Parameter(int32(5));

% This parameter object has the StorageClass property set to 'ExportedGlobal' vc_slprm = Simulink.Parameter(slexpr("a_vc+b_vc")); % Variant control for the vp_tunable parameter vc_startup = Simulink.VariantControl(Value=vc_slprm,ActivationTime="startup");

% These parameter objects have the CustomStorageClass property set to 'ExportToFile', HeaderFile property set to 'varparam.h', and DefinitionFile property set to 'varparam.c' slparam_1 = Simulink.Parameter(slexpr("a_vp+b_vp")); slparam_2 = Simulink.Parameter(slexpr("a_vp-b_vp")); slparam_3 = Simulink.Parameter(slexpr("a_vp*b_vp"));

vp_tunable = Simulink.VariantVariable('Choices', ... {'vc_startup==1','slparam_1','vc_startup==2','slparam_2','(default)','slparam_3'});

This code snippet shows the content of the external file external_imp_defines.h.

#define a_vc 1 #define b_vc 2 #define a_vp 6 #define b_vp 5

With Embedded Coder, you can generate code that defines a global variable and initializes it by using an expression involving system constants.

/* varparam.c / / Definition for custom storage class: ExportToFile / real_T slparam_1 = a_vp+b_vp; real_T slparam_2 = a_vp-b_vp; real_T slparam_3 = a_vpb_vp;

/* .c / / Exported block parameters */ int32_T vc_startup = a_vc+b_vc;

/* Model initialize function */ if (vc_startup == 1) { mVarParamVCtrlSlexpr_P.vp_tunable = slparam_1; } else if (vc_startup == 2) { mVarParamVCtrlSlexpr_P.vp_tunable = slparam_2; } else { mVarParamVCtrlSlexpr_P.vp_tunable = slparam_3; }

Similarly, in this model, vp_macro is a variant parameter that uses a Simulink.VariantControl object vc_cc with activation time set to code compile . Both vc_cc and the choices of vp_macro have the Define storage class that yields a macro in the generated code. The values of the parameter objects a_cc, b_cc, a_vp_cc, and b_vp_cc that are used in the expressions are imported from an external file external_imp_defines.h.

var-param-slexpr-macro-model.png

% These parameter objects have the CustomStorageClass property set to 'ImportedDefine' and HeaderFile property set to 'external_imp_defines.h' a_cc = Simulink.Parameter(int32(1)); b_cc = Simulink.Parameter(int32(2)); a_vp_cc = Simulink.Parameter(int32(7)); b_vp_cc = Simulink.Parameter(int32(4));

% This parameter object has the StorageClass property set to 'Define' and the HeaderFile property set to 'vcc_define.h' vc_slprm_cc = Simulink.Parameter(slexpr("a_cc+b_cc")); % Variant control for the vp_macro parameter vc_cc = Simulink.VariantControl(Value=vc_slprm_cc,ActivationTime="code compile");

% These parameter objects have the CustomStorageClass property set to 'Define' and the HeaderFile property set to 'varparam.h' slparam_1_cc = Simulink.Parameter(slexpr("a_vp_cc+b_vp_cc")); slparam_2_cc = Simulink.Parameter(slexpr("a_vp_cc-b_vp_cc")); slparam_3_cc = Simulink.Parameter(slexpr("a_vp_cc*b_vp_cc"));

vp_macro = Simulink.VariantVariable('Choices', ... {'vc_cc==1', 'slparam_1_cc', 'vc_cc==2', 'slparam_2_cc', '(default)', 'slparam_3_cc'});

This code snippet shows the content of the external file external_imp_defines.h.

#define a_cc 1 #define b_cc 2 #define a_vp_cc 7 #define b_vp_cc 4

For this variant parameter, Embedded Coder generates code that defines a macro whose value is an expression involving other macros.

/* vcc_define.h / / Definition for custom storage class: Define / #define vc_cc a_cc+b_cc / Referenced by: '/Gain1' */ #endif

/* varparam.h / / Definition for custom storage class: Define / #define slparam_1_cc a_vp_cc+b_vp_cc / Referenced by: '/Gain1' / #define slparam_2_cc a_vp_cc-b_vp_cc / Referenced by: '/Gain1' / #define slparam_3_cc a_vp_ccb_vp_cc

/* .c / / Model initialize function */ #if vc_cc == 1 mVarParamVCtrlSlexpr_P.vp_macro = slparam_1_cc; #elif vc_cc == 2 mVarParamVCtrlSlexpr_P.vp_macro = slparam_2_cc; #else mVarParamVCtrlSlexpr_P.vp_macro = slparam_3_cc; #endif

This example shows you how to specify the choice values of variant parameters as Simulink.Parameter variables. Here, using Simulink.Parameter variables allow you to generate code that imports variant parameter values from your existing user-written code. Thus, you can reuse the values that your existing code defines. During code compilation, the generated code and the user-written code is integrated into a single executable.

% imported parameters kv_hatchback = Simulink.Parameter(3); kv_sedan = Simulink.Parameter(6); kv_hatchback.CoderInfo.StorageClass = 'ImportedExtern'; kv_sedan.CoderInfo.StorageClass = 'ImportedExtern';

% variant control V = Simulink.VariantControl('Value',1,'ActivationTime','startup');

% variant parameters kv = Simulink.VariantVariable('Choices',{'V == 1','kv_hatchback',... 'V == 2','kv_sedan'});

For more information, see Reuse Variant Parameter Values from Handwritten Code Using Simulink.Parameter Variables (Embedded Coder).

This example shows you how to group variant parameters in the generated code by adding them to the same variant parameter bank (Simulink.VariantBank).

Create a variant parameter bank.

EngineParams = Simulink.VariantBank(Name='EngineParams',Description='Engine parameters',... VariantConditions={'V == EngType.Small','V == EngType.Big'});

Create a variant control variable V using the Simulink.VariantControl class. The Value property of this object allows you to select an active value for a variant parameter and the ActivationTime property allows you to specify a variant activation time. To use variant parameter banks, the activation time must be set up to startup. Use this variable to specify the variant condition expression for the choice values of a variant parameter.

Create two variant parameter objects K1 and K2 and associate them with the variant parameter bank EngineParams.

Use the name of the variant parameter bank to set the Bank property of a Simulink.VariantVariable object. The generated code groups the Simulink.VariantVariable objects that share the same Bank property in the same structure.

V = Simulink.VariantControl(Value=EngType.Small,ActivationTime='startup'); K1 = Simulink.VariantVariable(Choices={'V == EngType.Small',3,'V == EngType.Big',6},Bank='EngineParams'); K2 = Simulink.VariantVariable(Choices={'V == EngType.Small',[3, 6, 9],'V == EngType.Big',[5, 10, 15]},Bank='EngineParams');

To additionally specify code generation properties for a variant parameter bank, use the Simulink.VariantBankCoderInfo class.

Use a Parameter Writer block within an Initialize Function block to initialize the value of Simulink.VariantControl objects associated with a variant parameter.

You can use Parameter Writer blocks within an Initialize Function block to initialize the value of Simulink.VariantControl objects associated with a variant parameter (Simulink.VariantVariable object). The variant controls must have theirActivationTime property set to startup. This setup allows you to assign the variant control value on the model initialize event, thereby initializing the associated variant parameter. The variant controls and the variant parameter must be defined in the base workspace, model workspace, or data dictionaries linked to the model.

This model that implements a counter using a Unit Delay block uses a variant control V to specify the variant conditions for the variant parameter K. The Parameter Writer block inside theInitialize Function block sets the value of V. TheInitialize Function block also initializes the Unit Delay block state with the variant parameter K by using a State Writer block.

Model that uses an initialize function block to initialize the value of variant control V that is used by variant parameter K. Dialog boxes show the choices of variant parameter K and properties of variant control V.

When generating code for this model with Embedded Coder, the model initialize function initializes the variant controlV before the variant parameterK.

/* Model initialize function */
/* ParameterWriter: '<S1>/Parameter Writer' incorporates Constant: '<S1>/Constant1' */
 V = mVariantControlParamWriter_P.Constant1_Value_a;

/* Variant Parameters startup activation time */
  if (V == 1) {
    mVariantControlParamWriter_P.K = 10.0;
  } else if (V == 2) {
    mVariantControlParamWriter_P.K = 20.0;
  } else if (V == 3) {
    mVariantControlParamWriter_P.K = 30.0;
  }
  /* End of ParameterWriter: '<S1>/Parameter Writer' */
  /* StateWriter: '<S1>/State Writer' incorporates Constant: '<S1>/Constant' */
  mVariantControlParamWriter_DW.UnitDelay_DSTATE =
   mVariantControlParamWriter_P.K;

For applicable limitations, see Limitations.

Limitations

Extended Capabilities

expand all

For more information on code generation with variant parameters, see Options to Represent Variant Parameters in Generated Code (Embedded Coder).

Version History

Introduced in R2021a

expand all

You can generate code for variant parameter objects with variant activation time set tocode compile when they are defined in a data dictionary linked to a subsystem file. Previously, code generation was supported for such variant parameters only for variant activation times other than code compile.

You can simulate and generate code for models that define variant parameters in the model workspace. The ActivationTime property of theSimulink.VariantControl object used as the variant control variable for the variant parameter must be set to update diagram,update diagram analyze all choices, orstartup. The objects associated with the variant parameter object such as the Simulink.VariantControl object, theSimulink.Parameter object used to set theSpecification property, and any Simulink.Parameter objects used as values of the variant parameter must be defined in the model workspace along with the parameter.

For more information, see Configure Variant Parameter Values for Instances of Referenced Models.

You can use the Code Mappings editor to configure the code generation attributes for ERT-based and GRT-based targets for Simulink.VariantVariable andSimulink.VariantControl objects defined in the model workspace of a model. The objects are listed in the Code Mappings editor if:

For more information, see Configure Code Generation for Variant Parameters in Model Workspace Using Code Mappings Editor (Simulink Coder).

You can use the Bank property to add a Simulink.VariantVariable object to a variant parameter bank (Simulink.VariantBank).