Define, Configure, and Activate Variant Choices in a Variant Subsystem Block
Represent Variant Choices in a Variant Subsystem
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. A Variant Subsystem block allows you to use a combination of Subsystem, Model, or Subsystem Reference blocks as variant choices. For other ways to represent design variants, see Types of Variants in Simulink.
Add a Variant Subsystem block to your model and name it.
This block serves as the container for the variant choices.
Double-click the Variant Subsystem block. Add Inport and Outport blocks so that they match the inputs into and outputs from this block.
You can add only Inport, Outport, Subsystem, and Model blocks inside a Variant Subsystem block. You can pass control signals through data ports.
Right-click the badge on the Variant Subsystem block and select Block Parameters (Subsystem).
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 input and output ports as the containing Variant Subsystem block.
If your variant choices have different numbers of input and output ports, see Mapping Inports and Outports of Variant Choices in a Variant Subsystem.
Open each Subsystem block and create the model that represents a variant choice.
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.
Create a model that you want to include as a variant choice. Make sure that it has the same number of input and output ports as the containing Variant Subsystem block.
If your model has different numbers of input and output ports, see Mapping Inports and Outports of Variant Choices in a Variant Subsystem.
In your model, right-click the Variant Subsystem block that contains variant choices and select Block Parameters (Subsystem).
In the block parameters dialog box, click the button to add a Model block as variant choice.
Simulink creates an unresolved Model block in the Variant Subsystem block.
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.
At the MATLAB® command prompt, specify the control variables that create an activation condition when combined.
mode = 3; version = 2;
Right-click the Variant Subsystem block that is the container for variant choices in your model and select Block Parameters (Subsystem).
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.
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
Click Apply; otherwise, your changes are not saved.
Mapping Inports and Outports of Variant Choices in a Variant Subsystem
A Variant Subsystem block allows you to use a combination of Subsystem, Model, or Subsystem Reference blocks as variant choices. The inputs that the Variant Subsystem block receives from upstream model components map to the input and output ports of the variant choices.
The blocks representing variant choices can have input and output ports that differ in number from the input and output ports in the parent Variant Subsystem block. However, the following conditions must be satisfied:
The Variant choices must have same set of inports as the Variant Subsystem container block or it must be a subset of ports on the container block.
The Variant choices must have same set of outports as the Variant Subsystem container block or it must be a subset of ports on the container block.
The Variant choices must have same set of control port as the Variant Subsystem container block.
During simulation, Simulink disables the inactive ports in a Variant Subsystem block.
Convert to Variant Subsystem Block
In the Simulink Editor, you can convert these blocks to a Variant Subsystem 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:
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 Variant activation time. 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.