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 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
Label 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
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
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
objects as the variant controls in the block parameters dialog box of the
Variant Subsystem block.
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 custom 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
classdef sldemo_mrv_BUILD_TYPE < Simulink.IntEnumType enumeration PROTOTYPE (1) PRODUCTION (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.
The Variant Condition Legend is available only when Display > Blocks > Variant Condition is active.
By default, the Variant Condition Legend displays the variant condition annotation and the variant condition during simulation. To view the variant condition in the generated code, select the Show generated code conditions option on the Variant Condition Legend.
In the Variant Condition Legend, the variant conditions on
the blocks are annotated as
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.
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 (