Simulink.VariantControl - Create a variant control variable object - MATLAB (original) (raw)

Namespace: Simulink

Create a variant control variable object

Since R2021a

Description

The Simulink.VariantControl class creates a variant control variable object to associate the value of a variant control variable with a variant activation time. Use the Simulink.VariantControl object to determine the active choices of variant blocks and variant parameters during simulation.

To configure a Simulink.VariantControl object as a model argument, the object must be in a model workspace. In the Model Explorer, selectArgument for the variant control object. For more information, seeConfigure Instance-Specific Values for Block Parameters in a Referenced Model.

Creation

`variantControlVariable` = Simulink.VariantControl creates a variant control variable object with no value and'ActivationTime' set to 'update diagram'.

`variantControlVariable` = Simulink.VariantControl([Name,Value](#namevaluepairarguments)) creates a variant control variable object as specified by Name,Value pair arguments. Using this syntax, you create an object and associate its value with a variant activation time.

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Value', 1, 'ActivationTime', 'update diagram'

Value of variant control variable object, specified as any of these types:

When Value is set to a Simulink.Parameter object already used in the model or in another variant control object, use one of these syntaxes to avoid a compile-time error.

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

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

Example: 'Value',1

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

Example: P = Simulink.Parameter(1); V = Simulink.VariantControl('Value',copy(P));

Example: vc_slprm = Simulink.Parameter(slexpr("a_vc+b_vc")); vc_startup = Simulink.VariantControl(Value=vc_slprm,ActivationTime="startup");

Simulink® can set active choice of the variant blocks or variant parameters at different stages of the simulation and code generation workflow. Based on the stage you specify using this property, Simulink determines if the generated code must contain only the active choice or both active and inactive choices. The property also determines whether to analyze all the choices for incompatibilities in signal attributes. For more information on variant activation time, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.

When you specify the variant activation time as:

Example: 'ActivationTime', 'code compile'

Methods

expand all

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

ne Compare if the inputs are not equal.
eq Compares if the inputs are equal.
not Returns logical NOT of input.

Examples

collapse all

You can associate a variant control variable of type Simulink.VariantControl with a variant activation time. Simulink.VariantControl variant control variables help you switch variant elements such as blocks and parameters coherently.

Consider a group of blocks that represents a design choice, with each block having the same variant control variable of type Simulink.VariantControl. If you set the variant activation time of these blocks to inherit from Simulink.VariantControl, the blocks inherit the activation time from the variant control variable. As a result, all the blocks have the same activation time and are activated simultaneously to generate rational simulation results and valid code.

You can define Simulink.VariantControl type of control variables in storage locations as listed in Storage Locations for Variant Control Variables (Operands) in Variant Blocks.

Open the slexVariantSubsystems model.

open_system('slexVariantSubsystems');

In the block parameters dialog box of the Controller block:

1. Specify the variant controls, V == 1 and V == 2.

set_param('slexVariantSubsystems/Controller/Linear Controller', 'VariantControl', 'V==1') set_param('slexVariantSubsystems/Controller/Nonlinear Controller', 'VariantControl', 'V==2')

2. Set Variant activation time to inherit from Simulink.VariantControl to inherit the activation time from V.

set_param('slexVariantSubsystems/Controller', 'VariantActivationTime', 'inherit from Simulink.VariantControl')

vss-variantcontrol-dialog.png

In the base workspace, define a Simulink.VariantControl object, V. Set its value to 1, variant activation time to update diagram, and then simulate the model.

V = Simulink.VariantControl('Value', 1, 'ActivationTime', 'update diagram')

V = VariantControl with properties:

         Value: 1
ActivationTime: 'update diagram'

During simulation, the Controller block inherits the update diagram activation time from V and the Linear Controller block becomes active.

Double-click the Controller block to see the active choice.

sim ('slexVariantSubsystems');

If you change the value of V to 2, the Nonlinear Controller block becomes active during simulation. You can change the value of V using this command or from Simulink.VariantControl dialog box.

V.Value = 2; sim ('slexVariantSubsystems');

If you change the variant activation time of V to update diagram analyze all choices, the Controller block inherits the update diagram analyze all choices activation time from V. You can observe the inherited activation time using CompiledVariantActivationTime.

V.ActivationTime = 'update diagram analyze all choices'; sim ('slexVariantSubsystems'); get_param('slexVariantSubsystems/Controller', 'CompiledVariantActivationTime')

ans = 'update diagram analyze all choices'

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

You can define numeric control values in locations listed in Storage Locations for Variant Control Variables (Operands) in Variant Parameters.

Open the slexVariantParameters model.

open_system('slexVariantParameters')

In the MATLAB® Editor, specify variant choices in their simplest form as numeric values in Simulink.VariantVariable objects K1 and K2.

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

K1 = VariantVariable with 2 choices:

Condition Value


V == 1 3.5000 V == 2 8.5000

Specification: ''

Bank: ''

Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

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

K2 = VariantVariable with 2 choices:

Condition Value


V == 1 4.5000 V == 2 9.5000

Specification: ''

Bank: ''

Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Once you successfully create the Simulink.VariantVariable objects, you can modify them by using the methods described in Public Methods or from the VariantVariable dialog box.

add-variant-variable-ui.png

Activate one of the variant values by defining a control variable, V, and setting its value to 1 in a Simulink.VariantControl object.

V = Simulink.VariantControl('Value', 1, 'ActivationTime', 'update diagram')

V = VariantControl with properties:

         Value: 1
ActivationTime: 'update diagram'

When you simulate the model, the condition V == 1 evaluates to true. K1 is assigned a value of 3.5 and K2 is assigned a value of 8.5.

sim ('slexVariantParameters')

If you change the value of V to 2, Simulink® sets the value of K1 and K2 to 4.5 and 9.5 during simulation. You can change the value of V using this command or from Simulink.VariantControl dialog box.

V.Value = 2; sim ('slexVariantParameters')

Use enumerated types to give meaningful names to integers used as variant control values. For more information on enumerated type data, see Use Enumerated Data in Simulink Models.

In the MATLAB® Editor, define the classes that map enumerated values to meaningful names.

Open the slexVariantParameters model.

open_system('slexVariantParameters')

Specify the variant condition expressions V == EngType.Small and V == EngType.Big in Simulink.VariantVariable objects K1 and K2.

K1 = Simulink.VariantVariable('Choices', {'V == EngType.Small',3.5,'V == EngType.Big',8.5})

K1 = VariantVariable with 2 choices:

 Condition     	Value

V == EngType.Big 8.5000 V == EngType.Small 3.5000

Specification: ''

Bank: ''

Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

K2 = Simulink.VariantVariable('Choices', {'V == EngType.Small',4.5,'V == EngType.Big',9.5})

K2 = VariantVariable with 2 choices:

 Condition     	Value

V == EngType.Big 9.5000 V == EngType.Small 4.5000

Specification: ''

Bank: ''

Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Here, EngType is an integer-based enumeration class that is derived from the built-in data type, int32. The class has two enumeration values, Small and Big. These enumerated values have underlying integer values 1 and 2. In this example, the enumeration class is defined in the base workspace. You can choose to define the class in other storage locations as listed in Storage Locations for Variant Control Variables (Operands) in Variant Blocks.

classdef EngType < Simulink.IntEnumType enumeration Small (1) Big (2) end methods (Static) function retVal = addClassNameToEnumNames() % ADDCLASSNAMETOENUMNAMES Specifies whether to add the class name % as a prefix to enumeration member names in generated code. % Return true or false. % If you do not define this method, no prefix is added. retVal = true; end end end

Once you successfully create the Simulink.VariantVariable objects, you can modify them by using the methods described in Public Methods or from the VariantVariable dialog box.

add-variant-variable-ui-enum.png

Activate one of the variant values by defining the control variable V and setting its value to EngType.Small in Simulink.VariantControl object V.

V = Simulink.VariantControl('Value',EngType.Small,'ActivationTime','code compile')

V = VariantControl with properties:

         Value: Small
ActivationTime: 'code compile'

When you simulate the model, the condition V == 1 evaluates to true. K1 is assigned a value of 3.5, and K2 is assigned a value of 8.5.

sim('slexVariantParameters')

If you change the value of V to 2, Simulink® sets the value of K1 and K2 to 4.5 and 9.5 during simulation. You can change the value of V using this command or from Simulink.VariantControl dialog box.

V = VariantControl with properties:

         Value: 2
ActivationTime: 'code compile'

sim ('slexVariantParameters')

The code that you generate using enumerated types contains the names of the values rather than integers.

% slexVariantParameters_private.h % #if V == EngType_Big || V == EngType_Small % /* Variable: K1 Referenced by: '/Gain' / % #if V == EngType_Big % #define rtCP_Gain_K1 (8.5) % #elif V == EngType_Small % #define rtCP_Gain_K1 (3.5) % #endif % #endif % % #if V == EngType_Big || V == EngType_Small % / Variable: K2 Referenced by: '/Gain1' / % #if V == EngType_Big % #define rtCP_Gain1_K2 (9.5) % #elif V == EngType_Small % #define rtCP_Gain1_K2 (4.5) % #endif % #endif % #endif / RTW_HEADER_slexVariantParameters_private_h_ */

Note that for variant parameters with startup activation time, only enumerations that are defined using these techniques are supported:

These enumerations are also supported when permanently stored in a Simulink® data dictionary. See Enumerations in Data Dictionary.

If you intend to generate code for a model containing variant parameters, specify variant control variables as Simulink.Parameter objects. Simulink.Parameter objects allow you to specify other attributes, such as data type and storage class, and control the appearance and placement of variant control variables in generated code.

Open the slexVariantParameters model.

open_system('slexVariantParameters');

In the MATLAB® Editor, define a Simulink.Parameter object.

VSS_MODE = Simulink.Parameter; VSS_MODE.Value = 1; VSS_MODE.DataType = 'int32'; VSS_MODE.CoderInfo.StorageClass = 'Custom'; VSS_MODE.CoderInfo.CustomStorageClass = 'Define'; VSS_MODE.CoderInfo.CustomAttributes.HeaderFile ='demo_macros.h';

Variant control variables defined as Simulink.Parameter objects can have any of the storage classes listed in Storage Classes for Different Variant Activation Times (Simulink Coder).

You can also convert a scalar variant control variable into a Simulink.Parameter object. For more information, see Convert Variant Control Variables into Simulink.Parameter Objects.

Specify the object as a variant control in Simulink.VariantVariable objects K1 and K2.

K1 = Simulink.VariantVariable('Choices',{'V == 1',3.5,'V == 2',8.5}); K2 = Simulink.VariantVariable('Choices',{'V == 1',4.5,'V == 2',9.5});

Once you successfully create the Simulink.VariantVariable objects, you can modify them by using the methods described in Public Methods or from the VariantVariable dialog box.

add-variant-variable-ui.png

Activate one of the variant values by defining a Simulink.VariantControl object V and setting its Value property to VSS_MODE.

V = Simulink.VariantControl('Value',copy(VSS_MODE),'ActivationTime','code compile');

When you simulate the model, the condition V == 1 evaluates to true. K1 is assigned a value of 3.5, and K2 is assigned a value of 8.5.

sim('slexVariantParameters');

If you change the value of V to 2, Simulink® sets the value of K1 and K2 to 4.5 and 9.5 during simulation. You can change the value of V using this command or from the Simulink.VariantControl dialog box.

V.Value.Value=2; sim('slexVariantParameters');

Generate code from the model. For information on how to generate code, see Generate Code Using Embedded Coder (Embedded Coder)

The generated code contains both Linear and Nonlinear choices in preprocessor conditionals #if and #elif because of the code compile activation time. The variant control variable V is defined using a macro — #define directive — in the header file demo_macros.h. You can control the appearance and placement of V in the generated code and prevent optimizations from eliminating storage for V using the storage class property. For more information, see Storage Classes for Different Variant Activation Times (Simulink Coder).

% demo_macros.h % /* Exported data define / % % / Definition for custom storage class: Define / % #define V 2 / Referenced by: % * '/Gain' % * '/Gain1' % / % #endif / RTW_HEADER_demo_macros_h_ / % % /

Set the value of a Simulink.VariantControl object to an object of a user-defined data class that derives from Simulink.Parameter. The user-defined class package that contains the class definition must be available on the MATLAB® search path. Data objects that you create from your package can use the storage classes that the package defines. For an example that shows how to create a data class in a package, see Define Data Classes.

Explore the Model

In this example, the +myPackage folder is a user-defined data class package folder that contains the class folder @myExParameter. The class folder contains the class definition file myExParameter.m and the class derives from Simulink.Parameter. The csc_registration.m file contains the custom storage class defined for this package using the Custom Storage Class Designer.

open_system("slexVariantParameters");

  1. The slexVariantParameters model uses two variant parameters K1 and K2, and a Simulink.VariantControl object V, that are defined in the base workspace.
  2. Double-click the variant control object V in the base workspace to open the Simulink.VariantControl dialog box.
  3. Select the custom class myPackage.myExParameter from the Value type list.
  4. In the Value box, enter 1 as the value of the variant control.
  5. From the Data type list, select int32 and from the Activation time list, select startup.
  6. In the Code Generation tab, select the custom storage class myPackage_ExportedGlobal defined in the package.

simulink-variantcontrol-user-defined-class.png

Generate code from the model. For information on how to generate code, see Generate Code Using Embedded Coder (Embedded Coder).

The generated header file myParams.h contains the extern declaration for the variable V and the generated file myParams.c contains the definition of the variable according to the settings in the custom storage class definition.

/* myParams.h / / Declaration for custom storage class: myPackage_ExportedGlobal */ extern int32_T V;

/* myParams.c / / Exported data definition / / Definition for custom storage class: myPackage_ExportedGlobal */ int32_T V = 1;

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

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

Version History

Introduced in R2021a

expand all

Before R2025a, when you specified the value of a Simulink.VariantControl object with startup variant activation time as an integer-convertible double data type, Simulink implicitly converted the value to int32. In R2025a, this conversion is no longer performed. When you generate code for models that use such variant controls, the code generator declares the variant control variable to be ofdouble data type instead of int32.

You can set the Value property of aSimulink.VariantControl object to a Simulink.Parameter object that contains a mathematical expression specified using the slexpr function.

Simulink reports a compile-time error when you try to set the value of aSimulink.VariantControl object to a Simulink.Parameter object that is already used in the model or in another variant control object.

To avoid the error, set the value of a Simulink.VariantControl object by creating a separate Simulink.Parameter object or by creating a copy of an existing Simulink.Parameter object using the copy method. For example:

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

or

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

The Simulink.VariantControl object does not support setting itsValue property to a Simulink.Parameter object with Value set to the slexpr function or a nonscalar value.