Define, Configure, and Activate Variants

Represent Variant Choices

Variant choices are two or more configurations of a component in your model. This example shows how to represent variant choices inside a Variant Subsystem block in your model. For other ways to represent design variants, see Options for Representing Variants in Simulink.

  1. Add a Variant Subsystem block to your model and name it.

    This block serves as the container for the variant choices.

  2. Double-click the Variant Subsystem block. Add inport and outport blocks so that they match the inputs into and outputs from this block.

    Note

    You can add only Inport, Outport, Subsystem, and Model blocks inside a Variant Subsystem block. You can pass control signals through data ports.

  3. Right-click the badge on the Variant Subsystem block and select Block Parameters (Subsystem).

  4. In the block parameters dialog box, click the button for each variant subsystem choice you want to add.

    Simulink® creates empty Subsystem blocks inside the Variant Subsystem block. The new blocks have the same number of inports and outports as the containing Variant Subsystem block.

    Tip

    (If your variant choices have different numbers of inports and outports, see Mapping Inports and Outports of Variant Choices.)

  5. Open each Subsystem block and create the model that represents a variant choice.

  6. When you are prototyping variants, you can create empty Subsystem blocks with no inputs or outputs inside the Variant Subsystem block. The empty subsystem recreates the situation in which a subsystem is inactive without the need for completely modeling the variant. For an empty variant choice, either specify a variant activation condition or comment out the variant condition by placing a % symbol before the condition.

    If the empty variant choice is active during compilation, Simulink ignores it.

Include Simulink Model as Variant Choice

You can include a Simulink model as a variant choice inside a Variant Subsystem block.

  1. Create a model that you want to include as a variant choice. Make sure that it has the same number of inports and outports as the containing Variant Subsystem block.

    Note

    If your model has different numbers of inports and outports, see Mapping Inports and Outports of Variant Choices.

  2. In your model, right-click the Variant Subsystem block that contains variant choices and select Block Parameters (Subsystem).

  3. In the block parameters dialog box, click the button to add a Model block as variant choice.

    Simulink creates an unresolved model reference block in the Variant Subsystem block.

  4. Double-click the unresolved model block. In the Model name box, enter the name of the model you want to use as a model variant choice and click OK.

Configure Variant Controls

You can specify the conditions for activating a variant choice using variant controls. You can also specify at most one variant choice as the default.

  1. At the MATLAB® command prompt, specify the control variables that create an activation condition when combined.

    mode = 3;
    version = 2;

  2. Right-click the Variant Subsystem block that is the container for variant choices in your model and select Block Parameters (Subsystem).

  3. In the block parameters dialog box, in the Variant control column, select (default) next to one of the choices.

    Simulink verifies that only one variant choice is active for simulation. If Allow zero active variant controls is selected, you can have zero variant choice. When the control condition does not activate a variant, Simulink uses the default variant for simulation and code generation.

  4. Specify a variant condition each of the other choices. If you are using an empty variant choice, specify a variant condition for the choice. You can also comment out an existing activation condition by prefixing it with a % symbol.

  5. Click Apply; otherwise, your changes are not saved.

Convert to Variants

In the Simulink Editor, you can convert these blocks to a Variant Subsystem block:

  • Subsystem block

  • Model block

  • Variant Model block (for models created in versions earlier than R2017b)

  • Conditionally executed subsystems

To do so, right-click the block, then in the context menu, click Subsystem & Model Reference > Convert to > Variant Subsystem.

You can also convert these block to Variant Subsystem block programmatically. To do so, use any of these syntaxes:

  • Simulink.VariantManager.convertToVariant(gcb)

  • Simulink.VariantManager.convertToVariant(gcbh)

For example,

open_system('sldemo_variant_subsystems');
Simulink.VariantManager.convertToVariant('sldemo_variant_subsystems/Controller');

If you convert variant models to variant subsystem, note that the behavior of the Model block parameter Generate preprocessor conditionals is different than the Variant Subsystem block parameter Analyze all choices during update diagram and generate preprocessor conditionals. For variant models, enabling the parameter causes simulation and update diagram to compile the active variant only. For variant subsystem, enabling the parameter compiles all the variants, which can make simulation and updates slower.

Converting variant models to variant subsystems can require that you update scripts that use the Variants command-line parameter.

Related Examples

More About