Approaches to Control Active Variant Choice of a Variant Block Using Mask or Model Workspace
This example shows different approaches to control the active choice of inline variant and Variant Subsystem blocks from a mask or a model workspace. For more information on Variant blocks, seeWhat Are Variants and When to Use Them.
Model
In this example, four 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 blocks. 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.
限制在推荐的方法
Work only if theVariant control modeparameter is set to
Expression
and theVariant activation timeparameter is set toupdate diagram
. So, when you simulate or generate code for models with the recommended approaches, Simulink do not analyze signal attributes for consistency and also the generates code only for active choice.Do not support using
Simulink.Variant
objects orSimulink.Parameter
as variant control variables.Do not support using model arguments variables as variant control variables.
The number of characters in the variant control variable must not exceed 40.
The variant control variable originating from a mask workspace must not start with
SLMASK
.
Approach 1: Use Mask Parameter as a Variant Control Variable
本节解释如何控制the active variant choice of Variant Subsystem blocks and inline variant blocks by using a mask parameter.
Control Active Choice of a Variant Subsystem Block using Mask Parameter
Consider the model with Variant Subsystem block VSS1. The VSS1 subsystem specifies two potential variants, x2 and x3. The control expression for x2 isB == 1
and for x3 isB == 2
. The variableB
is a mask parameter. To view the properties ofB
:
1. Right-click the VSS1 subsystem.
2. SelectMask>Edit Mask. In theParameters & Dialogpane, underParameters,Promptcolumn specifies the label of the parameter on the mask dialog box, and theNamecolumn specifies the name of the mask parameter. In this example,Promptis specified asEnter the choice
, andNameis specified asB
.
3. To open the mask dialog box, double-click the VSS1 subsystem. During simulation, the index of the value that you specify here is mapped to the underlying variableB
, which is then used to evaluate the variant condition expressions associated with the block. In this example, the default value ofEnter the choiceis2
. When you simulate this model, the variant conditionB == 2
evaluates totrue
. The x2 subsystem becomes inactive, and the x3 subsystem becomes active.
4. To modify the active choice, specify the value as1
in the mask dialog box, then simulate the model again. During simulation, the value of theB
is set to1
which in turn evaluates the Variant condition,B == 1
totrue
. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.
Control Active Choice of Inline Variant Blocks using Mask Parameter
Consider the model with masked Subsystem block SS1. The SS1 subsystem has a mask parameterVC
that is used as a variant control variable in the underlying Variant Source block Vsrc. The first input port is associated with the control expressionVC == 1
and the second input port is associated with the control expressionVC == 2
. To view the properties ofVC
:
1. Right-click the SS1 subsystem.
2. SelectEdit Mask. In theParameters & Dialogpane, underParameters,Promptcolumn specifies the label of the parameter on the mask dialog box, and theNamecolumn specifies the name of the mask parameter. In this example,Promptis specified asChoose the signal
, andNameis specified asVC
.
3. To open the mask dialog box, double-click the SS1 subsystem. During simulation, the index of the value that you select here is mapped to the underlying variableVC
, which is then used to evaluate the variant condition expressions associated with the Vsrc block. In this example, the default value ofChoose the signalisSine Wave
. When you simulate this model, the index of theSine Wave
,2
, is mapped toVC
. The variant conditionVC == 2
evaluates totrue
. As a result, the first input port of the Vsrc block that is associated withVC == 1
becomes inactive and the second input port that is associated withVC == 2
becomes active.
4. To modify the active choice, select the value asConstant
in the mask dialog box, then simulate the model again. During simulation, the value of theVC
is set to1
which in turn evaluates the variant condition,VC == 1
totrue
. The first input port of the Vsrc block becomes active and the second input port becomes inactive.
Approach 2: Use Mask Initialization Variable as a Variant Control Variable
本节解释如何控制the active variant choice of Variant Subsystem blocks using a mask initialization variable. You can follow this approach to control the active variant choice of inline variant blocks as well.
Consider the model with Variant Subsystem block VSS2. 1. In the VSS2 subsystem, the control expression for x2 isA == 1
and for x3 isA == 2
. The variableA
used in the control expression is a regular MATLAB® variable that is defined in theInitializationtab of the mask workspace. To view the properties ofA
:
a. Right-click the VSS2 subsystem.
b. SelectMask>Edit Mask. In theInitializationtab, underInitialization commands,value ofA
is set to1
.
During simulation, this value is used to evaluate the variant condition expressions associated with the block. When you simulate this model, the variant conditionA == 1
evaluates totrue
. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.
3. To modify the active choice, specify the value ofA
as2
in theInitializationtab, then simulate the model again. During simulation,A == 2
evaluates totrue
. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.
Approach 3: Use Model Workspace Variable as a Variant Control Variable in a Variant Subsystem Block
本节解释如何控制the active variant choice of Variant Subsystem blocks using a model workspace variable. You can follow this approach to control the active variant choice of inline variant blocks as well.
Consider the model with Variant Subsystem block VSS3. 1. In the VSS3 subsystem, the control expression for x2 isC == 1
and forx3
isC == 2
. The variableC
used in the condition expression is a regular MATLAB® variable that is defined in the model workspace. To view the properties ofC
:
a. On theModelingtab, clickModel Explorer.
b. In theModel Hierarchypane, clickModel Workspace. The value ofC
is set to1
.
During simulation, this value is used to evaluate the variant condition expressions associated with the block. When you simulate this model, the variant conditionC == 1
evaluates totrue
. The x2 subsystem becomes active, and the x3 subsystem becomes inactive. To modify the active choice, specify the value ofC
as2
,n simulate the model again. During simulation, the Variant conditionC == 2
evaluates totrue
. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.
Approach 4: Use Mask Initialization Script to Control Active Variant Choices of a Variant Subsystem Block
This approach is not recommended for controlling the active variant choice of Variant Subsystems. However, if theVariant control modeof the subsystem is set toLabel
mode, you can follow this approach. For more information, seeMask a Variant Subsystem.
See Also
Control Active Choice of Locked Custom Library Variant Subsystem Using Mask Parameter