Working with Variant Choices
Each variant choice in your model is associated with a conditional expression called variant control. The way you specify your variant controls determines the active variant choice.Variant control modeparameter available in the block parameters dialog box allows you to selectExpression
orLabel
mode for modeling Variant blocks.
This image shows the block parameters dialog box of aVariant Subsystemblock that contains four variant choices:
The first choice is commented out by adding the
%
symbol before the variant control.The second choice is the
(default)
和is activated when no variant control evaluates totrue
.The third choice is activated when the expression
mode==3 && version==2
evaluates totrue
.The fourth choice is activated when the expression
mode==2 && version==1
evaluates totrue
.
Default Variant Choice
You can specify at most one variant choice as the default for the model. As shown in the image above, theLinear Controller
subsystem is defined as the default variant choice. During model compilation, if Simulink®finds that no variant control evaluates totrue
, it uses the default choice.
In the dialog box, select the variant choice and change itsVariant controlproperty to(default)
.
Active Variant Choice
While each variant choice is associated with a variant control, only one variant control can evaluate totrue
at a time. When a variant control evaluates totrue
, Simulink activates the variant choice that corresponds to that variant control. At most one variant choice can be active. The active variant cannot be changed once model is compiled.
In this example, you can activate either theModel
variant choice or theNonlinear Controller
variant choice by specifying appropriate values formode
和version
.
Value ofmode |
Value ofversion |
Active variant choice |
---|---|---|
2 |
1 |
Nonlinear Controller |
3 |
2 |
Model |
You can specify the values ofmode
和version
at the MATLAB®Command Window.
Inactive Variant Choice
When a variant control activates one variant choice, Simulink considers the other variant choices to be inactive. Simulink ignores inactive variant choices during simulation. However, Simulink continues to execute block callbacks inside the inactive variant choices.
The color of inactive choices fades by default. You can choose to disable the fading effect by using theVariant Fadingoption. TheVariant Fadingoption is available in theInformation Overlaysmenu on theDebugtab of the Simulink Editor. You can useget_param
和set_param
commands to view or change the fading state of inactive choices programmatically. For example,
get_param('bdroot,'VariantFading') % To view the fading state of inactive choices
set_param('bdroot,'VariantFading','on') % To turn on the fading effect of inactive choices
Empty Variant Choice
When you are prototyping variant choices, you can create emptySubsystemblocks with no inputs or outputs inside theVariant Subsystemblock. The empty subsystem recreates the situation in which that subsystem is inactive without the need for completely modeling the variant choice.
For an empty variant choice, you can either specify a variant activation condition or comment out the variant condition by placing a%
symbol before the condition.
If this variant choice is active during simulation, Simulink ignores the empty variant choice. However, Simulink continues to execute block callbacks inside the empty variant choices.
List of Variant Choices
You can get a list of variant choices in aVariant Subsystemblock using the read-only parameterVariantChoices
.
varchoices = get_param(gcb, 'VariantChoices');
In this example, theVariantChoices
parameter returns the following:
Open Active Variant
When you open a model, variant blocks display the name of the variant that was active the last time that you saved your model. Use theVariantmenu to open the active variant. Right-click the block and selectVariant>Open. Then select the active variant.
Use this command to find the current active choice:
get_param(gcb,'CompiledActiveChoiceControl')
Use this command to find the path to the current active choice:
get_param(gcb,'CompiledActiveChoiceBlock')
Note
The
CompiledActiveChoiceBlock
parameter is supported only for theVariant Subsystemblock.Active variant cannot be changed once the model is compiled.
相关的例子
- 定义,Configure, and Activate Variant Choices in a Variant Subsystem Block
- Automatic Mask Icon Promotion in Variant Subsystems