This example shows different approaches to control the active choice of a Variant Subsystem from a mask or a model workspace. For more information on Variant Subsystems, see Variant Subsystems.
To open the Simulink® model, type slexVariantSubsystemCtrlFromMaskandModelWks
in the
MATLAB® Command Window.
Three Simulink models are highlighted in green, and one model is highlighted in red. The models in green represent the recommended approaches to control the active choice of Variant Subsystems. These approaches let you limit the scope of the variant control variable, avoid name conflicts, and establish a clear ownership of the variable between Variant blocks. They also allow you to use the same names for variables in different scopes.
The recommended approaches:
Must be implemented only on Variant Subsystem blocks. The Variant Sink and the Variant Source blocks do not support these approaches.
Work only if the Variant control mode parameter
is set to expression
and the
Variant activation time parameter is set to
update diagram
.
Do not support using
objects or
Simulink.Variant
as variant
control variables.Simulink.Parameter
Do not support using model arguments variables as variant control variables.
Consider the model with Variant Subsystem block VSS1.
The VSS1 subsystem specifies two potential variants, x2
and x3
. The control expression for x2
is B == 1
and for x3
is B ==
2
. The variable B
is a mask parameter. To
view the properties of B
:
Right-click the VSS1 subsystem.
Select Mask > Edit Mask. In the Parameters &
Dialog pane, under
Parameters, the
Prompt column specifies the label of
the parameter on the mask dialog box, and the
Name column specifies the name of the
mask parameter. In this example, Prompt is
specified as Enter the choice
, and
Name is specified as
B
.
To open the mask dialog box, double-click the VSS1 subsystem. During
simulation, the value that you specify here is mapped to the underlying
variable B
, which is then used to evaluate the variant
condition expressions associated with the block.
In this example, the default value of Enter the
choice is 2
. When you simulate this model,
the variant condition B == 2
evaluates to
true
. The x2 subsystem becomes inactive, and the x3
subsystem becomes active.
To modify the active choice, specify the value as 1 in the mask dialog
box, then simulate the model again. During simulation, the value of the
B
is set to 1 which in turn evaluates the Variant
condition, B== 1
to true. The x2 subsystem becomes
active, and the x3 subsystem becomes inactive.
Consider the model with Variant Subsystem block VSS2.
In the VSS2 subsystem, the control expression for x2
is
A == 1
and for x3
is A ==
2
. The variable A
used in the control
expression is a regular MATLAB variable that is defined in the
Initialization tab of the mask workspace. To view
the properties of A
:
Right-click the VSS2 subsystem.
Select Mask > Edit Mask. In the Initialization tab, under
Initialization commands, the value of
A
is set to 1
.
During simulation, this value is used to evaluate the variant condition
expressions associated with the block. When you simulate this model, the
variant condition A == 1
evaluates to
true
. The x2 subsystem becomes active, and the x3
subsystem becomes inactive.
To modify the active choice, specify the value of A
as
2
in the Initialization tab,
then simulate the model again. During simulation, A == 2
evaluates to true
. The x2 subsystem becomes active, and
the x3 subsystem becomes inactive.
Consider the model with Variant Subsystem block VSS3.
In the VSS3 subsystem, the control expression for x2 is C ==
1
and for x3 is C == 2
. The variable
C
used in the condition expression is a regular
MATLAB variable that is defined in the model workspace. To view the
properties of C
:
On the Modeling tab, click Model Explorer.
In the Model Hierarchy pane, click
Model Workspace. The value of
C
is set to 1
.
During simulation, this value is used to evaluate the variant condition
expressions associated with the block. When you simulate this model, the
variant condition C == 1
evaluates to
true
. The x2 subsystem becomes active, and the x3
subsystem becomes inactive.
To modify the active choice, specify the value of C
as
2
, then simulate the model again. During simulation,
the Variant condition C == 2
evaluates to
true
. The x2 subsystem becomes active, and the x3
subsystem becomes inactive.
This approach is not recommended for controlling the active variant choice of
Variant Subsystems. However, if the Variant control
mode of the subsystem is set to label
mode, you can follow this approach. For more information, see Mask a Variant Subsystem.