Define and Configure Variant Sources and Sinks
Simulink® provides two blocks, Variant Source and Variant Sink, that you can use to visualize all possible implementations of variant choices in a model graphically. This example shows how to configure variant choices using the Variant Source and Variant Sink blocks.
Represent Variant Choices in Variant Source and Variant Sink Blocks
The variant conditions at the inports and outports of Variant
Source and Variant Sink blocks, respectively, determine
the activation and deactivation of the blocks connected to them. You can specify the
conditions for activating a variant choice using a variant
control. While each variant choice is associated with a variant
control, only one variant control can evaluate to true. When a variant control
true, Simulink activates the corresponding variant choice. You can also specify at
most one variant choice as the default choice.
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
Simulink.Parameter objects defined in the base workspace of the
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 Modes in Variant Blocks.
Add Variant Source and Variant Sink blocks to your model.
Using blocks from the Simulink Library Browser, create sources and sinks that represent variant choices. Connect choices to the input and output ports of the Variant Source and Variant Sink blocks.
In the MATLAB® Command Window, run these commands to create the variant control variables.
V = Simulink.Parameter(1); W = Simulink.Parameter(2);
Open the block parameter dialog box of the Variant Source block. Double-click the block or right-click the block and select Block Parameters (Variant Source).
The Ports and associated conditions table in the block parameter dialog box has a row for each variant choice connected to the input port of the Variant Source block. If there are no variant choices, the table is empty.
You can use buttons to the left of the table to modify the elements in the table.
Add a new input port: Create a new input port as a variant choice and add an entry for the new choice in the table.
Delete selected port: Delete the selected variant choice from the block and the variant choice entry from the table.
Create/Edit selected variant object: Create or edit a
Simulink.VariantExpressionobject for the selected variant choice in the base workspace and specify the variant condition using the
Simulink.VariantExpressionobject parameter dialog box.
For a model that uses the base workspace, this operation creates the
Simulink.VariantExpressionobject 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.
In the block parameters dialog box, in the Variant control expression column, type
V==1for one choice and
V==2for the other.
To specify one choice as the default variant, select
(default)from the Variant control expression list. If none of the variant controls evaluate to
true, Simulink uses the default variant for simulation and code generation.
Double-click the Variant Sink block. In the block parameters dialog box, in the Variant control expression column, type
W==1for one choice and
W==2for the other.
Simulate the model. Simulink propagates the variant conditions to identify which model components to activate.
You can visualize the conditions that activate each variant choice. In the Debug tab of toolstrip, select Information Overlays > Variant Conditions.
In the Variant Condition Legend dialog box, click through the hyperlinked variant condition annotations to observe which parts of the model each condition activates.