Main Content

Propagate Variant Conditions from Variant Source Blocks to Conditionally Executed Subsystems

A conditionally executed subsystem is a nonvirtual subsystem that allows you to control its execution with an external signal. Enabled, Triggered, and Function-Call Subsystems are examples of conditional subsystems. The external signal that controls a conditional subsystem is called the control signal and the port through which the signal enters the block is called the control port. For more information on conditional subsystems, see Conditionally Executed Subsystems Overview.

You can use a single-input/single-output or a multi-input Variant Source block to control the execution of conditional subsystems based on the active variant conditions at the inports of the Variant Source block.

Use Variant Source Block to Control Execution of Conditional Subsystem Blocks

This example shows how to use a single-input/single-output Variant Source block to control the execution of a conditional subsystem block.

Explore the Model

Open the model slexVariantControlPorts.

The model contains a single-input/single-output Variant Source block, Variant Source1, with variant condition set as V==1. The output from Variant Source1 block is connected to the control port of the Enabled Subsystem Subsystem.

The Allow zero active variant controls parameter on the Variant Source1 block is set to on. When this option is selected and there is no active variant choice, Simulink® disables all the blocks connected to the input and output stream of the Variant Source block.

When you simulate the model after setting V=1, the condition V==1 propagates to the outport of Variant Source1 and then to the control port of Subsystem. The condition assigned to the subsystem block then propagates to the blocks named Sine2 and Out1 connected to its input and output ports. If you set V to a value other than 1, Simulink disables all the blocks connected to the inport and outport of Variant Source1.

Propagate Variant Conditions to Function-Call Subsystems

A Function-Call Subsystem block is a subsystem that another block can invoke directly during simulation. The Function-Call Subsystem block is analogous to a function in procedural programming language. For more information, see Using Function-Call Subsystems.

You can use a Variant Source block to make the Function-Call Subsystem block conditional.

The Variant Source block has condition V==1, where V is a Simulink.Parameter.

The Output function call parameter on the Variant Source block is set to on. This enables the block to output function-call signals.

The Allow zero active variant controls parameter on the Variant Source block is set to on.

When you simulate this model, the variant condition from the Variant Source block propagates to the control port of the SubA subsystem block and further propagates to the blocks connected to its inports and outports.

  • When V=1, the SubA block is active and the variant condition propagates backward and forward to the blocks connected to the input (In1) and output (Out1) ports.

  • When V~=1 (for example, V=0), SubA becomes inactive, which makes Out1 inactive. In2 remains active because it is connected to SubB, which is active.

  • If In2 is not connected to SubB, In2 becomes inactive when V~=1.

Note: If the Function-Call Subsystem is placed inside a virtual grouped subsystem, the variant condition triggering the Function-Call Subsystem must match the corresponding condition on the input of the higher level subsystem block.

Use Variant Source Block to Receive or Output Function-Call Signals for Simulation and Code Generation

This example shows how a Variant Source block receives function-call signals at its inports and conditionally merges the signals at its outport. You can use such variant signals to control the execution of conditionally executed subsystems.

Explore the Model

Open the model slexVariantSourceFunctionCall.slx.

open_system('slexVariantSourceFunctionCall.slx');

The model contains a Variant Source block with the conditions A==1 and A==2 at its input ports. A is the variant control variable defined in the PostLoadFcn in File > ModelProperties > Callbacks. The variant conditions at the inports and outports of the Variant Source block determine the activation and deactivation of the blocks connected to it.

A Sine Wave block is connected to two MATLAB® Function blocks, Filter Positive inputs and Filter Negative inputs. The Filter Positive inputs block generates a function call when the Sine Wave block value is positive. The Filter Negative inputs block generates a function call when the Sine Wave block value is 0 or negative. The Variant Source block receives these function-call signals at its inports. The output port of the Variant Source block is connected to a Function-Call Subsystem block named Counter. The subsystem is triggered when A==1 and the Sine Wave block outputs positive values, and when A==2 and the Sine Wave block outputs negative values.

The Output function call parameter on the Variant Source1 block is set to on. This enables the block to receive and output function-call signals. To enable this option programmatically, use this command:

set_param('slexVariantSourceFunctionCall/Variant Source','OutputFunctionCall','on')

The Allow zero active variant controls parameter on the Variant Source1 block is set to on. When this option is selected and there is no active variant choice, Simulink disables all the blocks connected to the input and output stream of the Variant Source block.

Simulate the Model

To simulate the model, on the Simulation tab, click Run. Variant condition propagation determines the active and inactive variant choices.

  • When A==1, Filter Positive inputs is active.

  • When A==2, Filter Negative inputs is active.

In both cases, the count value in the Function-Call Subsystem, Counter, is incremented. If A~=1 and A~=2, then Counter does not execute because the Sine Wave block is inactive.

Related Topics