Main Content

Simulink.VariantControl class

Package: Simulink

Create a variant control variable object

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 value of variant parameter during simulation.

Creation

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

variantControlVariable = Simulink.VariantControl(Name,Value) 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.

Input Arguments

expand all

Name-Value Pair Arguments

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

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

Value of variant control variable object, specified as scalar variables, enumerated types, or Simulink.Parameter object.

Note

For a variant control variable object with startup activation time, specifying a value of enumerated type is not supported.

Example: 'Value', 1

Simulink® can set active value of the 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 values or both active and inactive values variant parameters. The property also determines whether to analyze all the values of the variant parameters for incompatibilities in signal attributes. For Activate Variant During Different Stages of Simulation and Code Generation Workflow.

When you specify the variant activation time as:

  • 'update diagram' –– Simulink does not analyze the values of variant parameters for incompatibilities in signal attributes. It generates code only for the active values of variant parameters.

  • 'update diagram analyze all choices' –– Simulink analyzes both active and inactive values of variant parameters for incompatibilities in signal attributes, however it generates code only for the active values of variant parameters.

  • 'code compile' –– Simulink analyzes both active and inactive values of variant parameters for incompatibilities in signal attributes and it also generates code for both active and inactive values of the variant parameters. The values are enclosed in C preprocessor conditional statements #if and #endif that are conditionally compiled when you compile the generated code.

  • 'startup' ––Simulink analyzes both active and inactive values of variant parameters for incompatibilities in signal attributes and it also generates code for both active and inactive values of the variant parameters. The values are enclosed in regular if conditions inside the model_initialize function of the generated code that are conditionally evaluated when you run the executable that is built from the code.

Example: 'ActivationTime', 'code compile'

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 in Storage 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 a Simulink.VariantVariable object.

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 to 1 in a Simulink.VariantControl object. This condition activates the value 1000 and assigns this value to vpObj.

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

             Value: 1
    ActivationTime: 'update diagram'

If you change the value of VCtrl to 2, Simulink® sets the value of vpObj to 2000.

VCtrl.Value = 2;

Use enumerated types to give meaningful names to integers used as variant control values.

You can define variant control variables of enumerated types in storage locations as listed in Storage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.

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

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.

type EngType.m
classdef EngType < Simulink.IntEnumType
        enumeration
        Small (1)
        Big (2)
        end
end

Specify the names as variant choices in a Simulink.VariantVariable object.

vpObj = Simulink.VariantVariable('Choices', {'VCtrl == EngType.Small',3.5,'VCtrl == EngType.Big',8.5})
vpObj = 
VariantVariable with 2 choices:

       Condition       	Value
______________________	_____
VCtrl == EngType.Big  	8.5000
VCtrl == EngType.Small	3.5000

Specification: ''

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

Activate one of the variant values by defining the control variable VCtrl and setting its value to EngType.Small in a Simulink.VariantControl object. This condition activates the value 3.5 and assigns this value to vpObj.

Note: For a variant control variable object with 'startup' activation time, specifying a value of enumerated type is not supported.

VCtrl = Simulink.VariantControl('Value',EngType.Small,'ActivationTime','code compile')
VCtrl = 
  VariantControl with properties:

             Value: Small
    ActivationTime: 'code compile'

Using enumerated types simplifies the generated code because it contains the names of the values rather than integers.

If you intend to generate code for a model containing variant parameter values, specify variant control variables as MATLAB® variables or Simulink.Parameter objects. Simulink.Parameter objects 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 type Simulink.Parameter only in the base workspace or in a data dictionary. Defining Simulink.Parameter type of variant control variables in the mask or model workspace is not supported. For more information on storage locations for variant control variables, see Storage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.

  • Simulink.Parameter objects within structures and that have data types other than Simulink.Bus objects are not supported.

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

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 as Simulink.Parameter objects can have one of these storage classes:

  • Define or ImportedDefine with 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 a Simulink.Parameter object. For more information, see Convert Variant Control Variables into Simulink.Parameter Objects.

Specify the object as a variant control using a Simulink.VariantVariable object.

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

Activate one of the variant values by defining a control variable VCtrl and setting its value to VSSMODE in a Simulink.VariantControl object. This condition activates the value 1000 and assigns this value to vpObj.

VCtrl = Simulink.VariantControl('Value',VSSMODE,'ActivationTime','code compile')
VCtrl = 
  VariantControl with properties:

             Value: [1x1 Simulink.Parameter]
    ActivationTime: 'code compile'

Introduced in R2021a