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==1
andA==2
at its input ports.A
is the variant control variable defined in thePreLoadFcn
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
andFilter Negative inputs
. TheFilter Positive inputs
block generates a function call when the value of the Sine Wave block is positive. TheFilter Negative inputs
block generates a function call when the Sine Wave block value is0
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 namedCounter
. The subsystem is triggered whenA==1
and the Sine Wave block outputs positive values, and whenA==2
and 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.
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. IfA~=1
andA~=2
, thenCounter
does not execute because the Sine Wave block is inactive.