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 conditional expression called 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.
A variant control is a Boolean expression that activates a specific variant choice
when it evaluates to
You can specify variant controls in the MATLAB® global workspace, mask workspace, or a data dictionary.
You can specify variant controls as
MATLAB expressions (including structures) or as expressions that contain one or
more of these operands and operators.
Variant control mode parameter available in the block
parameters dialog box allows you to select
switching mode for modeling Variant blocks.
Expression: Specifies the active Variant based on
the evaluation of the Variant conditions.
Variant control mode: Expression
Label: Specifies the name based Variant controls
(Label mode active choice). In
Label mode, Variant
control need not be created in the global workspace. Alternatively, you
can select the Label mode active choice from the command line. For
Variant control mode: Label
sim codegen switching: enables automatic variant
switching for simulation and code-generation workflows in variant
blocks. This feature is convenient to switch between simulation and
When you simulate (Normal , Accelerator, Rapid Accelerator) a model,
then Simulink automatically chooses the
sim branch as
the active choice. Similarly, when you do a Software-in-the-loop (SIL)
simulation, Processor-In-Loop (PIL) simulation or generate code or use
external mode, Simulink automatically chooses the
If a variant block has Variant control mode
expression, then using
codgen for its
choice condition is not supported.
Variant control mode: sim codegen switching
In the Variant activation time drop-down
list, you can choose
update diagram or
update diagram analyze all choices.
For data signals,
update diagram analyze all
choices ensures that the signal attributes (data
types, dimensions, etc.) between both choices are consistent.
Here is a sample screen showing a variant block switched to sim choice.
Variable names that resolve to MATLAB variables or
Simulink.Parameter objects with
integer or enumerated data type and scalar literal values
Variable names that resolve to
Scalar literal values that represent integer or enumerated values
Parentheses for grouping
Arithmetic, relational, logical, or bitwise operators
For more information, see Operators and Operands in Variant Condition Expressions.
When you compile the model, Simulink determines that a variant choice is active if its variant control
true. The evaluation of active variant happens in
the early stages of compilation and the active variant cannot be changed once model
Simulink variant objects within structures are not allowed.
Simulink parameters within structures are not allowed.
You can use many approaches for switching between variant choices—from options to use while prototyping to options required for generating code from your model.
|Scalar variable||Rapid prototyping|
|Reuse variant conditions|
|Generate preprocessor conditionals for code generation|
|Enumerated type||Improved code readability because condition values are represented as meaningful names instead of integers|
You can find control variables using the function
Scalar MATLAB variables allow you to rapidly prototype variant choices when you are still building your model. They help you focus more on building your variant choices than on developing the expressions that activate those choices.
Consider a model that contains two variant choices, each represented by a Variant Subsystem block.
You can specify variant controls in their simplest form as scalar variables in the block parameters dialog box of the Variant Subsystem block.
The Condition field for both the
Nonlinear Controller are
N/A, because the variant control itself is the
You can activate one of the variant choices by defining a scalar variable
V and setting its value to
1 at the
MATLAB Command Window.
V = 1;
Linear Controllervariant choice. Variant controls are ignored when
%symbol is used. Similarly, if variant control is empty, the choice is ignored.
Similarly, if you change the value of
2, Simulink activates the
Nonlinear Controller variant
Simulink.VariantObjects for Variant Condition Reuse
After identifying the variant choices that your model requires, you can
construct complex variant conditions to control the activation of your variant
choices. Define variant conditions as
Simulink.Variant objects enable you to reuse common variant
conditions across models and help you encapsulate complex variant condition
Consider an example where variant controls are already defined in the global workspace.
You can convert these controls into condition expressions encapsulated as
You can then specify these
Simulink.Variant objects as the
variant controls in the block parameters dialog box of the Variant
The Condition field now reflects the encapsulated variant condition. Using this approach, you can develop complex variant condition expressions that are reusable.
Simulink.ParameterObjects or MATLAB variables for Code Generation
If you intend to generate code for a model containing variant choices, specify
variant control variables as MATLAB variables or
Simulink.Parameter objects allow you to specify other
attributes (such as data type) that are required for generating
VSSMODE = Simulink.Parameter; VSSMODE.Value = 1; VSSMODE.DataType = 'int32'; VSSMODE.CoderInfo.StorageClass = 'Custom'; VSSMODE.CoderInfo.CustomStorageClass = 'ImportedDefine'; VSSMODE.CoderInfo.CustomAttributes.HeaderFile =... 'rtwdemo_importedmacros.h';
Simulink.Parameterobjects can have one of these storage classes:
with header file specified
Your own storage class that defines data as a macro
You can also convert a scalar variant control variable into a
Simulink.Parameter object. See Convert Variant Control Variables into Simulink.Parameter Objects.
Use enumerated types to give meaningful names to integers used as variant control values.
In the MATLAB Editor, define the classes that map enumerated values to meaningful names.
classdef sldemo_mrv_CONTROLLER_TYPE < Simulink.IntEnumType enumeration NONLINEAR (1) SECOND_ORDER (2) end end
Simulink.Variant objects for these classes in
the global workspace.
VE_NONLINEAR_CONTROLLER = Simulink.Variant... ('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.NONLINEAR') VE_SECOND_ORDER_CONTROLLER =Simulink.Variant... ('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.SECOND_ORDER') VE_PROTOTYPE =Simulink.Variant... ('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PROTOTYPE') VE_PRODUCTION =Simulink.Variant... ('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PRODUCTION')
Using enumerated types simplifies the generated code because it contains the names of the values rather than integers.
If Variant Legend is not available, on the Debug tab, select Information Overlays > Variant Conditions.
By default, the Variant Condition Legend displays the variant condition annotation, the variant condition during simulation, and the source of the variant condition variables. To view the variant condition in the generated code, select the Show generated code conditions option in the Variant Condition Legend window.
In the Variant Condition Legend, the variant conditions on the blocks are
v is the variant
semantic indicator and
c represents the variant condition index.
You can click through the hyperlinked variant annotations to observe which parts of
the model the condition corresponds to.
When you hover over a block that has a variant condition, the tooltip displays the variant annotation and the related variant condition for the block. To view the variant condition annotation tooltip, the Variant Condition option must be selected.
In the legend, the source of the variant condition variables are also displayed. The variables can originate from a mask, model, or a base workspace. The variables originating from different mask workspaces can have the same name and have different values. To observe the source of the variables, click the hyperlinked workspaces.
To view the Variant Condition Legend programmatically,
Simulink.VariantManager.VariantLegend function in the
MATLAB command window.
Simulink evaluates condition expressions within variant controls to determine the active variant choice. You can include the following operands in a condition expression:
Simulink.Parameter objects that are not structures
and that have data types other than
Parentheses for grouping
Variant condition expressions can contain MATLAB operators, provided the expression evaluates to a boolean value. In
B are expressions that
evaluate to an integer, and
x is a constant integer
|MATLAB Expressions That Support Generation of Preprocessor Conditionals||Equivalent Expression in C Preprocessor Conditional|
If the value of the
second operand (
If the value of the
second operand (
The net variant condition is the total of the local condition and its ancestral condition.
Consider this model
slex_netvariant with two Single-input Single-Output (SISO)
Variant Source blocks,
Variant Source and
Variant Source1 with variant conditions as
When you simulate this model, the
Variant Source1 block and
other blocks within the Subsystem block will have a local condition
W==1 propagated from the
block. The ancestral condition
V==1 is propagated from the
Variant Source block onto the Subsystem block.
Therefore, the net variant condition on the
Variant Source1 block
and other blocks within the Subsystem block will be