Route among multiple inputs using Variants
Simulink / Signal Routing
The Variant Source block has one or more input ports and one output port. You can define Variant choices as blocks that are connected to the input port so that, at most, one choice is active.
Each input port is associated with a Variant control. The Variant control that
evaluates to true
, determines which input port is active.
When the Variant activation time option in the block dialog box is set
to update diagram
, then during simulation Simulink® connects the active choice directly to the output port of the
Variant Source block and ignores the inactive choices.
Port_1
— Input port associated with first Variant controlInput port associated with the first Variant control. The Variant
control that evaluates to true
, determines which
input port is active.
Data Types: single
| double
| half
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
| enumerated
| bus
Port_N
— Input port associated with Nth Variant controlInput port associated with the Nth Variant control. The Variant
control that evaluates to true
, determines which
input port is active.
Data Types: single
| double
| half
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
| enumerated
| bus
Port_1
— Output of active VariantOutput signal from the active Variant.
Data Types: single
| double
| half
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
| enumerated
| bus
Variant control mode
— Name of Variant control modeexpression
(default) | label
| sim codegen switching
expression
— To choose the active
Variant based on the evaluation of the Variant conditions, use
expression
mode. The Variant control
variables used in the Variant condition must be created in a global
workspace or in a data dictionary.
Label
— To choose the active Variant
based on the name of the Variant you specify in the Label
mode active choice parameter, use
label
mode. In
label
mode, the Variant control is a string
and does not need to be created in any workspaces.
When you select label
mode, the Variant
badge indicates the change.
Note
When you promote the Label mode active choice parameter to a mask, Variant control mode is disabled.
If the block is in
expression
mode while
promoting the Label mode active
choice parameter to a mask, you can
change the Variant control mode
to label
by changing the
promoted Label mode active
choice parameter from the Mask dialog
box.
If the block is in
label
mode while
promoting the Label mode active
choice parameter to a mask, you cannot
change the Variant control mode
to expression
mode.
For information about promoting parameters to masks, see Promote Parameter to Mask.
sim codegen switching
— To automatically
switch between the Variants for simulation and code generation
workflows, use sim codegen switching
mode. When you simulate (Normal , Accelerator, Rapid Accelerator) a
model, then Simulink automatically chooses the
sim
branch as the active choice. Similarly,
when you do a Software-in-the-loop (SIL), Processor-In-Loop (PIL)
simulation or generate code or use external mode, Simulink
automatically chooses the codegen
branch.
When you set this parameter to different values, the Variant badge changes as shown in Variant Badges.
The Variant activation time parameter is
available only when you set the Variant control
mode parameter to expression
or sim codegen switching
.
Variant activation time
— Determine when Simulink chooses active variant choiceupdate diagram
(default) | update diagram analyze all choices
| code compile
This parameter determines if Simulink sets the active choice of a Variant Source block during update diagram or code compile. This parameter also determines which variability to include in the generated code for ERT targets. If you choose to include all the Variant choices, the choices are enclosed within C preprocessor conditional statements (#if and #endif) in the generated code.
When you set this parameter to different values, the variant badge changes as shown in Variant Badges.
You can set the Variant activation time parameter as:
update diagram
— Simulink sets the active choice during update diagram
before the propagation of signal attributes. Inactive choices
are removed prior to propagation of signal attributes, so the
generated code contains only active choice.
update diagram analyze all choices
—
Simulink sets the active choice during update diagram after
the propagation of signal attributes. Signal attributes are
propagated to both active and inactive choices. All choices are
analyzed to ensure consistency of all variant branches between
the results of simulation and code generation. Inactive choices
are removed at the end of update diagram before model start
occurs. The generated code contains only active choices. This
workflow remains the same for simulation and code
generation.
code compile
— For simulation, the
workflow is same as the update diagram analyze all
choices
. However, the inactive choices are not
removed during code generation. The generated code contains
active and inactive choices, and the choices are enclosed in C
preprocessor conditional statements, #if
and
#endif
.
This figure shows the Variant activation time for the different values you specify in this parameter.
For more information, see Represent Variant Source and Sink Blocks in Generated Code (Embedded Coder).
When you set the Variant control mode
to expression
, this parameter can
be set to update diagram
,
update diagram analyze all
choices
, or code
compile
.
When you set the Variant control mode
to sim codegen switching
, this
parameter can be set to either update
diagram
or update diagram
analyze all choices
. The code
compile
option becomes unavailable.
Block Parameter:
VariantActivationTime
|
Type: character vector |
Values:
update diagram | update
diagram analyze all choices |code
compile
|
Default:
update diagram
|
Port
— Number of connected input portNumber of the input port that is connected to one Variant choice upstream of the Variant Source block. This value is read-only.
Click to add a port or
to delete an existing one.
Variant control expression
— Variant controls available in the global workspace'Variant'
(default) | boolean condition expression | Simulink.Variant
object | Simulink.Parameter
object | enumDisplays the Variant controls available in the global workspace. The
Variant control can be a Boolean condition expression or a
Simulink.Variant
object representing a Boolean
condition expression. If you want to generate code for your model, you must
define the control variables as Simulink.Parameter
objects.
To enter non-numeric Variant control values, use enumerated data. For information about using enumerated data, see Use Enumerated Data in Simulink Models
To edit a Variant name, double-click a Variant control expression cell and type in the Variant control expression. Click Apply after you edit a Variant control name. If you add or delete a Variant control without applying the changes, the previous edits on the Variant control name are lost.
The Variant control that evaluates to
true
determines which input port must be
active.
Block Parameter:
VariantControls
|
Type: cell array of character vectors |
Value: Variant control that is associated with the Variant choice |
Default:
'Variant'
|
Condition (read-only)
— Condition for Variant controlsDisplays the Condition for the Variant controls that
are Simulink.Variant
objects. Create or change a Variant
condition in the Simulink.Variant
parameter dialog box or
in the global workspace.
For more information, see Create Variant Controls Programmatically and Simulink.Variant
.
Label mode active choice
— Name of Variant to use if Label control mode is selectedChoice_1
(default) | name of Variant controlWhen you select the Variant control mode as
Label
, the Label mode active
choice option is available. You can select an active Variant
choice from Label mode active choice options. You can
also right-click the badge on the Variant Source block and
select Label Mode Active Choice.
The Label mode active choice drop-down list displays
all Variant controls that are currently defined in the global workspace or a
data dictionary. Use valid MATLAB® identifiers to specify Variant controls. For more information,
see Simulink.Variant
.
Note
Label mode active choice option is not available
in Expression
mode.
To enable this parameter, select Label
mode.
Block Parameter:
LabelModeActivechoice
|
Type: character vector |
Value: Specified by the Variant control expression. |
Default:
Choice_1 |
Allow zero active variant controls
— Simulate model without active Variant choiceoff
(default) | on
To simulate a model (containing a Variant block) without an active Variant choice, select the Allow zero active variant controls option. 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 Variant Source block. The removed blocks are ignored from update diagram or simulation.
If you do not select this option, Simulink generates an error when there is no active Variant choice.
When you select this option, the Variant badge indicates the change.
Expression
option from Variant
control mode is selected.
Block Parameter:
AllowZeroVariantControls
|
Type: character vector |
Values:
'off' | 'on'
|
Default:
'off'
|
Show variant condition on block
— Annotate block portsoff
(default) | on
When you select this option, Simulink annotates each Variant control (condition expression) on the Variant Source block ports.
Block Parameter:
ShowConditionOnBlock
|
Type: character vector |
Value:
'off' | 'on'
|
Default:
'off'
|
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Manual Variant Sink | Manual Variant Source | Model | Simulink.Variant
| Variant Sink | Variant Subsystem