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 the block.
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 the Variant Subsystem block.
Open the block parameter dialog box. Right-click the badge on the Variant Subsystem block and select Block Parameters (Subsystem).
Tip
For information on all parameters on the Variant Subsystem block, see Variant Subsystem, Variant Model.
The Variant choices table in the block parameter dialog box shows the variant choices contained in the Variant Subsystem. You can use buttons to the left of the table to add or modify elements in the table.
Action Button Create and add a new subsystem choice: Create a new Subsystem block as a variant choice and add an entry for the new choice in the table. Create and add a new model variant choice: Create a new Model block as a variant choice and add an entry for the new choice in the table. Create/Edit selected variant object: Create or edit a
Simulink.Variant
object in the global workspace and specify the variant condition using theSimulink.Variant
object parameter dialog box.Note
For a model that uses the base workspace, this operation creates the
Simulink.Variant
object in the base workspace, and the object is available only for the current MATLAB session. To permanently store the data, save the object in a MAT file or MATLAB script.Open selected variant choice block: Open the Subsystem block for the selected row. Refresh dialog information from Variant Subsystem contents: Update the Variant choices table according to the variant choices and values of the variant control in the global workspace.
Include Subsystem Block as Variant Choice
To include Subsystem block as a variant choice inside the Variant 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.
Tip
If your variant choices have different numbers of input and output ports, see Map Inports and Outports of Variant Choices in Variant Subsystem.
Open each Subsystem block and create the model that represents a variant choice.
When you prototype 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.
Note
If your model has different numbers of input and output ports, see Map Inports and Outports of Variant Choices in 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 a 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. While each variant choice in a model is associated with a
variant control, only one variant control can evaluate to true
.
You can also specify at most one variant choice as the default.
You can choose the mode in which you want to specify the variant controls using
the Variant control mode parameter. This example uses the
expression
mode. Here, we specify the variant
controls as Boolean condition expressions that contain normal MATLAB® variables defined in the base workspace of the model.
Tip
For information on different variant control modes, see Introduction to Variant Controls.
For information on the types of variant control variables supported in variant condition expressions, see Types of Variant Control Variables (Operands) in Variant Blocks and Types of Variant Control Variables (Operands) in Variant Parameters.
In the MATLAB Command Window, specify the variant 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)
for one choice.Specify a variant condition for each of the other choices. If you are using an empty variant choice, specify a variant condition for the choice or comment out an existing activation condition by placing a
%
symbol before the condition.Simulink evaluates the variant controls and verifies that only one variant choice is active during simulation. If none of the variant controls evaluate to
true
, Simulink uses the(default)
variant for simulation and code generation.If Allow zero active variant controls is selected, you can have zero active variant choices in the Variant Subsystem. When you select this option and there is no active variant choice, Simulink simulates the model without any active variant choice in the Variant Subsystem. This parameter is available only if there are no
(default)
variant choices in the Variant Subsystem and if you set Variant control mode toexpression
.Click Apply to save the changes.
Map Inports and Outports of Variant Choices in 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 that represent 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 met:
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.
If the Variant Subsystem container block has control ports:
The type of control port blocks in all the variant choices must be same as the Variant Subsystem block. For example, you cannot use Enabled Subsystem and Function-Call Subsystem blocks as choices within a Variant Subsystem block.
The control port on the Variant Subsystem block and the corresponding control ports on its variant choices must have the same name. For example, if the name of the control port on the Variant Subsystem is
fcn
, then the name of the corresponding control ports on all its variant choices must also befcn
.
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:
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:
For example:
addpath(fullfile(matlabroot,'examples','simulink_variants','main')); 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 Variant activation time. For variant models, enabling the Generate preprocessor conditionals parameter causes simulation and update diagram to compile the active variant only. For variant subsystems, enabling the parameter compiles all the variants, which can slow down simulation and updates.
Converting variant models to variant subsystems can require that you update
scripts that use the Variants
command-line parameter.