Variant Control Mode in Variant Parameters
The components of a Simulink® model that contain variants are activated or deactivated based on the variant choice that you select.
Each variant choice in your model is associated with a variant
control. Variant controls determine which variant choice is active. By changing
the value of a variant control, you can switch the active variant choice. While each variant
choice is associated with a variant control, only one variant control can evaluate to
true
. When a variant control evaluates to true
,
Simulink activates the variant choice that corresponds to that variant control. For a
simple example, see Introduction to Variant Controls.
Switch Between Choices Using Condition Expressions in Variant Parameters
In expression
mode, Simulink chooses the active variant based on the evaluation of the variant conditions.
When a condition expression evaluates to true
, the corresponding variant
choice becomes active. When a condition expression evaluates to false
,
the corresponding variant choice becomes inactive.
Note
You can simulate and generate code from the model containing a variant parameter
with Variant control mode set to
expression
mode. The generated code can contain active and
inactive choices that are enclosed in preprocessor conditionals #if
and #elif
, or regular if
conditions.
With expression
type of variant control mode:
Use
code compile
activation time to generate a code that contains active and inactive choices of the model in C preprocessor conditionals#if
and#elif
. The preprocessor conditionals are conditionally compiled for the active choice that you provide as an input to the compiler. For more information, see Compile Code Conditionally for All Values of Variant Parameters with Same and Different Dimensions (Simulink Coder).Use
startup
activation time to:Improve the speed of iterative simulations and Simulink Test™ workflows using fast restart. For more information, see Run Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart.
Generate a code that contains active and inactive choices of the model in regular
if
conditions. Theif
condition is evaluated and conditionally executed for the active choice that you provide as an input to the executable. For more information, see Run Executable for Different Variant Parameter Values Without Recompiling Code.
Use expression
Type of Variant Controls in Variant Parameters
To specify the variant condition expressions in variant parameters, use the Simulink.VariantVariable
object. 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. For more information, see Create a Simple Variant Parameter Model.
K1 = Simulink.VariantVariable('Choices', {'VSS_MODE == 1', 3.5, 'VSS_MODE == 2', 8.5})
Types of Variant Controls in expression
Mode
In variant parameters, the expression
type of variant
controls can be:
Boolean condition expression for rapid prototyping. For example,
A == 1
,A ~= B
,A && B == 1
, and so on.A
Simulink.Variant
object that contains a condition expression for condition reuse. See Simulink.VariantExpression Objects for Variant Condition Reuse of Variant Parameters.
Here, A
and B
are operands called as
variant control variables. ==
,
~=
, and &&
are operators in the condition
expression. The condition expression can contain one or more such variant control
variables and operators. For information on supported types and storage location of
variant control variables, see Types of Variant Control Variables (Operands) in Variant Parameters and Storage Locations for Variant Control Variables (Operands) in Variant Parameters. For information on
operators, see Types of Operators in Variant Parameters.
Types of Variant Control Variables (Operands) in Variant Parameters
In expression
mode, you can specify the variant control
variables as any of these types, from options to use while prototyping to options required
for generating code from your model.
Specification | Purpose | Example |
---|---|---|
Simulink.VariantControl object | Associate a variant activation time | V = Simulink.VariantControl(Value=1,ActivationTime="code compile") |
Simulink.Parameter object | Generate preprocessor conditionals for code generation | VSS_MODE = Simulink.Parameter(1);
V = Simulink.VariantControl(Value=copy(VSS_MODE),ActivationTime="code compile") VSS_MODE is a Simulink.Parameter object.
See Simulink.Parameter Type of Variant Control Variables for Code Generation of Variant Parameters. |
User-defined data class object | Use custom type that inherits from
| Custom Data Class Objects as Values of Simulink.VariantControl Objects |
Simulink Enumerations | Improved code readability because condition values are represented as meaningful names instead of integers | V = Simulink.VariantControl(Value=EngType.Small,ActivationTime="code compile") EngType is an integer-based enumeration class and
Small is the enumerated value. See Enumerated Types to Improve Code Readability of Variant Control Variables of Variant Parameters. |
Simulink.Variant object | Reuse variant conditions | V = Simulink.VariantControl(Value=1,ActivationTime="update diagram"); LinearController=Simulink.VariantExpression("V==1"); NonLinearController=Simulink.VariantExpression("V==2"); K1 = Simulink.VariantVariable('Choices',{'LinearController',3.5,'NonLinearController',8.5}); LinearController and
NonLinearController are
Simulink.VariantExpression objects used to encapsulate
variant conditions. See Simulink.VariantExpression Objects for Variant Condition Reuse of Variant Parameters. |
For list of all examples, see Use Variant Control Variables in Variant Parameters.
Storage Locations for Variant Control Variables (Operands) in Variant Parameters
You can define the variant control variables in different storage locations based on your requirement.
Note
All the variant control variables in a variant parameter must originate from the same storage location.
Storage Location | Use for Storage Location | Supported Types of Variant Control Variables (See Types of Variant Control Variables (Operands) in Variant Parameters) | For More Information |
---|---|---|---|
Base workspace | Store variables while you experiment with temporary models |
Simulink.VariantExpression , Simulink.Parameter ,
enumerated type, and Simulink.VariantControl | Temporary Data: Base Workspace |
Data dictionary | Permanently store global data, share data between models, and track changes made to data. | Simulink.VariantExpression ,
Simulink.Parameter , enumerated type, and
Simulink.VariantControl | What Is a Data Dictionary? |
For capabilities and advantages of each storage location, see Determine Where to Store Variables and Objects for Simulink Models.
Types of Operators in Variant Parameters
Variant condition expressions can contain MATLAB® operators, provided the expression evaluates to a boolean value. In variant parameters, the operators that you can use to form a variant condition expression are:
Parentheses for grouping
Relational Operators and Logical Operators as listed in this table.
This table lists the supported MATLAB operators and their equivalent in the generated code. In these examples,
A
andB
are expressions that evaluate to an integer.Supported MATLAB Expressions Equivalent Expression in Generated C Code Relational A == B
A == B
A ~= B
A != B
Logical ~A
!A
, whereA
is not an integerA && B
A && B
A || B
A || B
Evaluate Variant Condition Expressions at Different Variant Activation Times
In expression
mode, you can specify if Simulink must evaluate condition expressions during model compile, simulation-loop,
code compile, or model start up stage of simulation and code generation workflow using
variant activation times. For information on stages of simulation and code generation, and
supported variant activation times in expression
mode, see
Activate Variant During Different Stages of Simulation and Code Generation Workflow.
Variant Activation Times for Different Variant Control Modes in Variant Parameters
This table explains the variant activation time supported for different variant control modes in variant parameters. For information on variant activation times, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.
Variant control mode | Variant activation time | ||||
---|---|---|---|---|---|
update diagram | update diagram analyze all choices | code compile | startup | runtime (Variant Subsystem block
only) | |
expression | ✓ | ✓ | ✓ | ✓ | ✓ |
label | not supported | not supported | |||
sim codegen switching | not supported | not supported |