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. The Variant control mode parameter available in the block parameters dialog box allows you to select Expression or Label mode for modeling Variant blocks.

This image shows the block parameters dialog box of a Variant Subsystem block 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) and is activated when no variant control evaluates to true.

  • The third choice is activated when the expression mode==3 && version==2 evaluates to true.

  • The fourth choice is activated when the expression mode==2 && version==1 evaluates to true.

Default Variant Choice

You can specify at most one variant choice as the default for the model. As shown in the image above, the Linear Controller subsystem is defined as the default variant choice. During model compilation, if Simulink® finds that no variant control evaluates to true, it uses the default choice. Simulink issues a warning if the default variant choice is never active, helping identify potentially unintended configurations within the model.

A variant condition is set to false if the blocks associated with that variant condition are never active.

In the dialog box, select the variant choice and change its Variant control property to (default).

Active Variant Choice

While each variant choice is associated with a variant control, only one variant control can evaluate to true at a time. When a variant control evaluates to true, 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 the Model variant choice or the Nonlinear Controller variant choice by specifying appropriate values for mode and version.

Value of modeValue of versionActive variant choice
21Nonlinear Controller
32Model

You can specify the values of mode and 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 the Variant Fading option. The Variant Fading option is available in the Information Overlays menu on the Debug tab of the Simulink Editor. You can use get_param and 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 empty Subsystem blocks with no inputs or outputs inside the Variant Subsystem block. 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 a Variant Subsystem block using the read-only parameter VariantChoices.

varchoices = get_param(gcb, 'VariantChoices');

In this example, the VariantChoices parameter 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 the Variant menu to open the active variant. Right-click the block and select Variant > 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 the Variant Subsystem block.

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

Related Examples

More About