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 modelslexVariantSourceFunctionCall.slx.

open_system('slexVariantSourceFunctionCall.slx');

The model contains a Variant Source block with the conditionsA==1andA==2at its input ports.Ais the variant control variable defined in thePreLoadFcnmodel 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 inputsandFilter Negative inputs. TheFilter Positive inputsblock generates a function call when the value of the Sine Wave block is positive. TheFilter Negative inputsblock generates a function call when the Sine Wave block value is0or 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 namedCounter. The subsystem is triggered whenA==1and the Sine Wave block outputs positive values, and whenA==2and the Sine Wave block outputs negative values.

TheOutput function callparameter on theVariant Source1块设置为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')

TheAllow zero active variant controlsparameter on theVariant Source1块设置为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 theSimulationtab, clickRun. Variant condition propagation determines the active and inactive variant choices.

  • WhenA==1,Filter Positive inputsis active.

  • WhenA==2,Filter Negative inputsis active.

In both cases, the count value in the Function-Call Subsystem,Counter, is incremented. IfA~=1andA~=2, thenCounterdoes not execute because the Sine Wave block is inactive.