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.
Structure of Variant Condition Expression in Variant Parameters
A variant condition expression consists of:
Variant control variables — Named variables defined in locations such as the base workspace, model workspace, or a data dictionary.
Operators — MATLAB® logical operators (
&&,||,~) and relational operators (==,~=) to combine or compare variant control variables and constants. The types of operators you can use depend on the ActivationTime parameter. For more information, see Types of Operators in Variant Parameters.Comparison values — Values to compare against variant control variables. These can be any valid MATLAB data type, including logical, enumeration, or other appropriate types.
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 compileactivation time to generate a code that contains active and inactive choices of the model in C preprocessor conditionals#ifand#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
startupactivation 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
ifconditions. Theifcondition 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 (Embedded Coder).
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.VariantExpressionobject that contains a condition expression for condition reuse, and use them for AUTOSAR code generation workflows. 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. For information on
operators, see Types of Operators in Variant Parameters.
Types of Variant Control Variables (Operands) in Variant Parameters
The Value property of the variant control variable (Simulink.VariantControl), which determines the active value of a variant parameter,
must be one of these types:
Integer
Boolean
Enumerated value
Simulink.Parameterobject with value of type integer, Boolean, enumeration, or a mathematical expression specified using theslexprfunctionUser-defined type that inherits from
Simulink.Parameter
You can define variant control variables in different storage locations based on your requirements. If you define a variable with the same name in the base workspace and the model workspace, Simulink evaluates the variable first in the model workspace, then in the base workspace. Simulink gives priority to the definition in the closest context, so the most relevant value is used to evaluate variant condition expressions. For details on the capabilities and advantages of each storage location, see Determine Where to Store Variables and Objects for Simulink Models.
Note
All variant control variables in a variant parameter must originate from the same workspace.
Use a
Simulink.SimulationInputobject to temporarily set values of variant control variables for each simulation run. The object is useful withstartupvariants in applications like testing, iterative simulation, and rapid prototyping, because you do not need to change workspace variables.
You can specify the value of the variant control variables as any of these types, from options to use while prototyping to options required for generating code from your model. For the examples on how to use different types of variant control variables in variant parameters, see Use Variant Control Variables in Variant Parameters
| Type of variant control variable | Workspace supported | update diagram | update diagram analyze all choices | code compile | startup |
|---|---|---|---|---|---|
See Use Simulink.VariantControl Variables for Coherent Switching of Choices in Variant Blocks. | base workspace data dictionary | ✓ | ✓ | ✓ | ✓ |
model workspace | ✓ | ✓ | ✓ | ||
See Use Custom Data Class Objects as Values of Simulink.VariantControl Objects. | base workspace data dictionary | ✓ | ✓ | ✓ | ✓ |
| model workspace | ✓ | ✓ | ✓ | ||
| base workspace data dictionary | ✓ | ✓ | ✓ | ✓ |
model workspace | ✓ | ✓ | ✓ | ||
See Use Simulink.Parameter Variant Control Variables for Code Generation of Variant Parameters. | base workspace data dictionary | ✓ | ✓ | ✓ | ✓ |
| model workspace | ✓ | ✓ | ✓ | ||
Numeric values — Rapid prototyping and simulation for quick variant switching. See Use Numeric Variant Control Values for Rapid Prototyping of Variant Parameters. | base workspace data dictionary | ✓ | ✓ | ✓ | ✓ |
model workspace | ✓ | ✓ | ✓ |
Tip
To provide descriptive names for comparison values and restrict them to a specific, predefined set in variant condition expressions, use enumerations as described in Use Enumerated Types to Improve Code Readability of Variant Control Variables of Variant Parameters.
When generating code for ERT-based and GRT-based targets, use Code Mappings Editor — C (Simulink Coder) to configure code generation attributes for signals, states, and parameters including the
Simulink.Parameterobjects. Using the editor centralizes control over how these elements appear in the generated code. For more information, see Configure Code Generation for Variant Parameters in Model Workspace Using Code Mappings Editor (Simulink Coder).
How to Choose the Value of Simulink.VariantControl in Variant Parameters
Use this flowchart to determine the appropriate data type for the value of the
Simulink.VariantControl type of variant control variable in variant
parameters.
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,
AandBare expressions that evaluate to an integer.Supported MATLAB Expressions Equivalent Expression in Generated C Code Relational A == BA == BA ~= BA != BLogical ~A!A, whereAis not an integerA && BA && BA || BA || 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
| |
expression | ✓ | ✓ | ✓ | ✓ | not supported |
label | not supported | not supported | |||
sim codegen switching | not supported | not supported | |||