Main Content

Propagate Variant Conditions Outside a Variant Subsystem Block

When you specify variant conditions on a Variant Subsystem block, Simulink® propagates these conditions to determine which components of the model are active during simulation. By default, these conditions do not propagate outside the variant subsystem. You can use the Propagate conditions outside of variant subsystem block parameter to allow the conditions to propagate outside the Variant Subsystem block to the connecting blocks.

Note

The variant condition annotations displayed on model components help you visualize the propagated conditions.

To view the variant condition annotations, on the Debug tab, select Information Overlays > Variant Legend.

If Variant Legend is not available, on the Debug tab, select Information Overlays > Variant Conditions.

Consider this model containing a Variant Subsystem block named Controller with two variant choices, Linear Controller and Nonlinear Controller. A specific variant condition activates each block.

The variant choices in theController block do not have the same interface. The inports sensor1 and sensor3 are used in Linear Controller and Nonlinear Controller, but sensor2 is used only in Linear Controller. Hence, the sensor2 block is active only when Linear Controller is active and is not executed for the other choice. To make the model components outside the Variant Subsystem (Controller) aware of the active or inactive state of blocks within the Variant Subsystem block, the block conditions must propagate outside the boundaries of the block.

Note

Variant conditions do not propagate outside a Variant Subsystem block if all variant choices within the block have the same interface.

Propagate Conditions Outside a Variant Subsystem

To propagate conditions outside of a variant subsystem, select Propagate conditions outside of variant subsystem in the Block Parameters dialog box of the Variant Subsystem block. By default, this parameter is set to off.

Variant Condition Propagation Outside a Variant Subsystem for update diagram Activation Time

When the Variant activation time parameter for a Variant Subsystem block is set to update diagram, only the active choice is analyzed to determine the conditions propagated out of the variant subsystem.

In the example, when you set the active variant choice to Nonlinear Controller, sensor2 is inactive. The variant annotation v2 with condition set to false is displayed on the sensor2 port and the inactive inport block In2 that is connected to it. In the Variant Conditions Legend, a condition is set to false if the blocks associated with that condition are never active.

When you set the active variant choice to Linear Controller, the sensor2 port and the inport block In2 are unconditional because the Linear Controller is using all the input signals.

The code that you generate contains only the active choice. In this example, the input port In2 is inactive and so it is not part of the generated code.

Variant Condition Propagation Outside a Variant Subsystem for code compile Activation Time

For any variant activation time other than update diagram, all variant choices are analyzed to determine the conditions propagated out of the variant subsystem.

In the example model, set the Variant activation time parameter to code compile.

The conditions are propagated outside the Variant Subsystem block for both the variant choices.

The code that you generate contains both active and inactive choices enclosed in preprocessor conditionals #if and #elseif. In this example, the input port In2 is guarded with necessary conditions.

Note

To propagate variant conditions outside a Variant Subsystem block with zero active variant control specified, select Allow zero active variant controls in the Block Parameters dialog box of the Variant Subsystem block.

Condition Propagation on a Variant Subsystem Without Inport and Outport Blocks

Consider this model with a Variant Subsystem block with two variant choices. There are no inports or outports on the block. The two variant choices have the conditions Var==1 and Var==2, respectively. When the Propagate conditions outside of variant subsystem parameter of the Variant Subsystem block is set to off, a logical OR of the variant conditions, Var==1 || Var==2, is set on the Variant Subsystem block. When the parameter is set to on, no conditions are applied on the Variant Subsystem block.

Variant Subsystem with no inports and outports

Propagate Conditions Programmatically

To propagate conditions outside of a Variant Subsystem block programmatically, use one of these syntaxes:

  • Propagate conditions:

    set_param(VariantSubsystemName, 'PropagateVariantConditions','on')

    For example,

    set_param('sldemo_variant_subsystems/Controller','PropagateVariantConditions','on')
  • Propagate conditions and generate preprocessor conditionals in code:

    set_param(VariantSubsystemName,'PropagateVariantConditions',...
    'on','VariantActivationTime','code compile')

    For example,

    set_param('slexVariantSubsystems/Controller','PropagateVariantConditions',...
    'on','VariantActivationTime','code compile')

Adaptive Interface for Variant Subsystems

This example shows how propagating variant conditions outside a Variant Subsystem block enables the block to adapt its interface according to the state of the underlying blocks.

Explore the Model

Open the model slexVariantSubsystemsAdaptiveInterface.

The Controller block is a Variant Subsystem that provides a Linear and a Nonlinear choice. The Linear choice is active when V = 1, Nonlinear choice is active when V = 2. V is a variant control variable of type Simulink.Parameter and is defined in the PostLoadFcn callback of the model.

To change the value of the variant control variable, in the MATLAB Command Window, type V.Value = 1 or V.Value = 2.

Double-click the Controller block to view its contents. The Linear and Nonlinear choices do not have a similar interface. The Linear choice is using the sensor1 and sensor3 inputs of the Controller block. The Linear choice is not using the sensor2 input and does not produce a saturate output.

In the Block Parameters dialog box for the Controller block, the Propagate condition outside of variant subsystem check box is selected. This setting propagates the conditions outside the block and enables the Variant Subsystem block to adapt its interface according to the state of the underlying blocks.

Simulate the Model

Simulate the model after setting the active choice to Linear. The condition V = 2 propagates to the In2, filter, and saturation logger blocks. These blocks become inactive because the Linear choice is not using the sensor2 input and is not writing to the saturate output of the Controller block.

Limitations

Propagated variant conditions from variant subsystems can be set on Simscape™ or Stateflow® blocks only for update diagram variant activation time.

Note

All elements of a Mux, Demux, or a Vector Concatenate block signal must have the same variant condition.

Related Topics