Main Content

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 selectExpressionorLabelmode 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 expressionmode==3 && version==2evaluates totrue.

  • The fourth choice is activated when the expressionmode==2 && version==1evaluates 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 Controllersubsystem 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 totrueat 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 theModelvariant choice or theNonlinear Controllervariant choice by specifying appropriate values formodeversion.

Value ofmode Value ofversion Active variant choice
2 1 Nonlinear Controller
3 2 Model

You can specify the values ofmodeversionat 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_paramset_paramcommands 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, theVariantChoicesparameter returns the following:

List of variant choices in the variant subsystem

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

  • TheCompiledActiveChoiceBlockparameter is supported only for theVariant Subsystemblock.

  • Active variant cannot be changed once the model is compiled.

相关的例子

More About