Approaches to Control Active Variant Choice of a Variant Subsystem

This example shows different approaches to control the active choice of a Variant Subsystem from a mask or a model workspace. For more information on Variant Subsystems, see Variant Subsystems.

Model

To open the Simulink® model, type slexVariantSubsystemCtrlFromMaskandModelWks in the MATLAB® Command Window.

Three Simulink models are highlighted in green, and one model is highlighted in red. The models in green represent the recommended approaches to control the active choice of Variant Subsystems. These approaches let you limit the scope of the variant control variable, avoid name conflicts, and establish a clear ownership of the variable between Variant blocks. They also allow you to use the same names for variables in different scopes.

Limitations in Recommended Approaches

The recommended approaches:

  • Must be implemented only on Variant Subsystem blocks. The Variant Sink and the Variant Source blocks do not support these approaches.

  • Work only if the Variant control mode parameter is set to expression and the Variant activation time parameter is set to update diagram.

  • Do not support using Simulink.Variant objects or Simulink.Parameter as variant control variables.

  • Do not support using model arguments variables as variant control variables.

Approach 1: Use Mask Parameter as a Variant Control Variable

  1. Consider the model with Variant Subsystem block VSS1.

    The VSS1 subsystem specifies two potential variants, x2 and x3. The control expression for x2 is B == 1 and for x3 is B == 2. The variable B is a mask parameter. To view the properties of B:

    1. Right-click the VSS1 subsystem.

    2. Select Mask > Edit Mask. In the Parameters & Dialog pane, under Parameters, the Prompt column specifies the label of the parameter on the mask dialog box, and the Name column specifies the name of the mask parameter. In this example, Prompt is specified as Enter the choice, and Name is specified as B.

  2. To open the mask dialog box, double-click the VSS1 subsystem. During simulation, the value that you specify here is mapped to the underlying variable B, which is then used to evaluate the variant condition expressions associated with the block.

    In this example, the default value of Enter the choice is 2. When you simulate this model, the variant condition B == 2 evaluates to true. The x2 subsystem becomes inactive, and the x3 subsystem becomes active.

  3. To modify the active choice, specify the value as 1 in the mask dialog box, then simulate the model again. During simulation, the value of the B is set to 1 which in turn evaluates the Variant condition, B== 1 to true. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.

Approach 2: Use Mask Initialization Variable as a Variant Control Variable

  1. Consider the model with Variant Subsystem block VSS2.

    In the VSS2 subsystem, the control expression for x2 is A == 1 and for x3 is A == 2. The variable A used in the control expression is a regular MATLAB variable that is defined in the Initialization tab of the mask workspace. To view the properties of A:

    1. Right-click the VSS2 subsystem.

    2. Select Mask > Edit Mask. In the Initialization tab, under Initialization commands, the value of A is set to 1.

    During simulation, this value is used to evaluate the variant condition expressions associated with the block. When you simulate this model, the variant condition A == 1 evaluates to true. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.

  2. To modify the active choice, specify the value of A as 2 in the Initialization tab, then simulate the model again. During simulation, A == 2 evaluates to true. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.

Approach 3: Use Model Workspace Variable as a Variant Control Variable

  1. Consider the model with Variant Subsystem block VSS3.

    In the VSS3 subsystem, the control expression for x2 is C == 1 and for x3 is C == 2. The variable C used in the condition expression is a regular MATLAB variable that is defined in the model workspace. To view the properties of C:

    1. On the Modeling tab, click Model Explorer.

    2. In the Model Hierarchy pane, click Model Workspace. The value of C is set to 1.

    During simulation, this value is used to evaluate the variant condition expressions associated with the block. When you simulate this model, the variant condition C == 1 evaluates to true. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.

  2. To modify the active choice, specify the value of C as 2, then simulate the model again. During simulation, the Variant condition C == 2 evaluates to true. The x2 subsystem becomes active, and the x3 subsystem becomes inactive.

Approach 4: Use Mask Initialization Script to Control Active Variant Choices

This approach is not recommended for controlling the active variant choice of Variant Subsystems. However, if the Variant control mode of the subsystem is set to label mode, you can follow this approach. For more information, see Mask a Variant Subsystem.

Related Topics