Main Content

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 input ports and conditionally merges the signals at its output port. 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 PreLoadFcn model callback. The variant conditions at the input and output ports 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 value of the Sine Wave block 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 input ports. 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.