Main Content

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 evaluates to 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 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 Modes in Variant Blocks.

  1. Add Variant Source and Variant Sink blocks to your model.

  2. 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.

  3. In the MATLAB® Command Window, run these commands to create the variant control variables.

    V = Simulink.Parameter(1);
    W = Simulink.Parameter(2);
    

  4. 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).

    Tip

    For information on all block parameters, see Variant Source and Variant Sink.

    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.

    ActionButton

    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.

    the add a new input port button

    Delete selected port: Delete the selected variant choice from the block and the variant choice entry from the table.

    the delete selected port button

    Create/Edit selected variant object: Create or edit a Simulink.VariantExpression object for the selected variant choice in the base workspace and specify the variant condition using the Simulink.VariantExpression object parameter dialog box.

    Note

    For a model that uses the base workspace, this operation creates the Simulink.VariantExpression 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.

    the Create or Edit selected variant object button
  5. In the block parameters dialog box, in the Variant control expression column, type V==1 for one choice and V==2 for 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.

  6. Click Apply.

  7. Double-click the Variant Sink block. In the block parameters dialog box, in the Variant control expression column, type W==1 for one choice and W==2 for the other.

  8. Click Apply.

  9. Simulate the model. Simulink propagates the variant conditions to identify which model components to activate.

  10. You can visualize the conditions that activate each variant choice. In the Debug tab of toolstrip, select Information Overlays > Variant Conditions.

  11. In the Variant Condition Legend dialog box, click through the hyperlinked variant condition annotations to observe which parts of the model each condition activates.

Related Examples

More About