| Contents | Index |
| On this page… |
|---|
Variant Subsystem Block Overview Example of a Variant Subsystem Block Configuring the Variant Subsystem Block Disabling and Enabling Subsystem Variants |
A variant subsystem provides multiple implementations of a subsystem, with only one implementation active during simulation. You can programmatically swap the active implementation with another, without modifying the model.

A Variant Subsystem block consists of multiple subsystems as follows:
Each subsystem has a variant object that you associate with it.
The subsystem whose variant object condition is true becomes the active variant.
A Variant Subsystem block can have only one active variant.
The active variant is the subsystem used for simulation.
The specifications of the Variant Subsystem block parameters dialog box must include:
The name of a variant object, in the Variant object column of the Variant choices table.
The variant object must have a conditional expression that evaluates to true or false.
Instructions for specifying a variant subsystem are in Configuring the Variant Subsystem Block.
To view the example model, either click AutoSSVar.mdl, or execute:
addpath([docroot '/toolbox/simulink/ug/examples/variants/mdlref/']);
open('AutoSSVar.mdl');

An icon
appears in the lower-left
corner to indicate that the block uses variants.
The name of the variant that was active the last time you saved the model appears at the top of the block.
When you change the active variant and update the diagram, the variant block name changes.
When you open the example model, a callback function loads a MAT-file that populates the base workspace with the variables and objects that the model uses. The base workspace contains the variant control variables and variant objects.

The example model illustrates the same application as the Example of a Model Variants Block, but using variant subsystems instead of variant referenced models. Both examples show the use of variants for the following cases:
An automobile that can use either a diesel or a gasoline engine
Each engine must meet either the European or American (USA) emission standard
The example model, AutoSSVar, implements the application using a Variant Subsystem block, named Engine. The Engine block consists of a set of four subsystems. Each subsystem represents one permutation of engine fuel and emission standards.
Double-click the Variant Model block, Engine, to view the child subsystems.

The subsystem diagram for a Variant Subsystem block has no connections. The only blocks allowed in the Variant Subsystem block diagram are Inport, Outport, and Subsystem blocks. If you are generating code for a Variant Subsystem block, see Restrictions on Code Generation of a Variant Subsystem in the Embedded Coder documentation.
To view how to specify these variants, from the model diagram window, right-click the Engine block, and select Subsystem Parameters or select Edit > Subsystem Parameters.
View the Variant choices (list of child subsystems) table in the dialog box, as shown in the following figure.

For instructions, see Configuring the Variant Subsystem Block.
To add and configure a Variant Subsystem block to specify your variants, do the following:
From the Simulink Library Browser, add a Variant Subsystem block to your model:
Simulink > Ports and Subsystems > Variant Subsystem block
To configure your variants, open the Variant Subsystem block dialog box. From the model diagram window, right-click the Variant Subsystem block, Engine, and select Subsystem Parameters or select Edit > Subsystem Parameters.
The Variant Subsystem block parameters dialog box opens.

Add a new subsystem choice in the Variant Subsystem
block. In the Variant choices table, click the Create
and add a new subsystem choice button
. A new variant choice
appears in the table and a new Subsystem block appears
in the Variant Subsystem block diagram window.
Rename the subsystem in the Variant Subsystem block diagram window, and open and configure the subsystem as desired. You can drag and drop existing subsystems into the new variant subsystem.
Return to the Variant Subsystem parameter dialog box. In the Variant object column of the Variant choices table, specify the variant name in the row of the subsystem you just configured. Double-click the default name, then type the name of the variant object. For an existing variant object, Simulink retrieves the variant condition for the object when you press Enter or click away from the Variant object. In the Variant choices table, double-click the Variant object field in the row of the subsystem you just configured. Specify the name for the Variant object.
To specify the Condition that
determines which variant is active, click the Create/Edit
selected variant button
. For the selected variant
choice, you create a variant object in the base workspace if necessary,
or edit an existing variant. The Simulink.Variant parameter dialog
box opens.

Specify the Condition for the variant object. For example, FUEL==2 && EMIS==2.
When the variant object condition evaluates to true at compile time, the subsystem associated with the variant becomes the active variant.
Click Apply to check the expression, then click OK.
The Variant choices table is updated with the condition for the new variant object. Simulink also created the variant object as a Simulink.Variant object in the base workspace. If you use a variant control variable in the Condition expression, you must create the variant control variables in the base workspace.
To add another variant, click the Create
and add a new subsystem choice button
![]()
Repeat the previous steps until you have specified all your variant subsystems and their associated variant objects.
For more information on the block controls, see the Variant Subsystem block page.
Click OK to close the Variant Subsystem dialog box.
Note Your variant conditions might require you to define control variables in the base workspace before they can be evaluated. See Creating Control Variables and Saving Variant Components. |
For next steps, see Selecting the Active Variant. The model diagram does not display the new active variant on the block until you update the diagram.
You can disable individual subsystem choices. To ignore a subsystem for simulation and code generation, you can comment out the variant object in the Variant Choices table as follows:
In the Variant choices table, double-click the name of the variant object.
Add a '%' to the beginning of the variant object name.
Click Apply and OK.
To enable the subsystem variant again, remove the %' from the variant object name.
You can also override variants and specify the active variant. See Overriding Variant Conditions.
A Variant Subsystem block must meet the following requirements:
The Inport, Outport, and Connection Port blocks in the Variant Subsystem block must be identical to the corresponding inports and outports of its child subsystems.
Inside the Variant Subsystem block diagram, you cannot create any lines connecting blocks.
You can nest Variant Subsystem blocks to any level. The hierarchy resulting from nesting must satisfy all applicable requirements and limitations when you compile the model or generate code for it.
Note Requirements and limitations that apply to code generation are in Variant Systems. |
To see a demo that shows how to define a variant subsystem, click sldemo_variant_subsystems.mdl. In the Help Browser, select: Simulink > Demos > Modeling Features > Subsystems > Variant Subsystems.
![]() | Setting Up Model Variants | Setting Up Variant Control | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |