Variable-Size Signal Basics
A Simulink® signal can be a scalar, vector (1-D), matrix (2-D), or N-D. A Simulink variable-size signal is a signal whose size (the number of elements in a dimension), in addition to its values, can change during a model simulation. However, during a simulation, the number of dimensions cannot change. This capability allows you to model systems with varying resources, constraints, and environments.
You can create variable-size signals in your Simulink model by using:
Switch or Multiport Switch blocks with different input ports having fixed-size signals with different sizes. The output is a variable-size signal.
A Selector block with the
Starting and ending indices (port)
indexing option. The index port signal can specify different subregions of the input data signal which produce an output signal of variable size as the simulation progresses.An S-Function block with the output port configured for a variable-size signal. The output includes not only the values but also the dimension of the signal.
A MATLAB Function block with the output configured for a variable-size signal.
How Variable-Size Signals Propagate
In the Simulink environment, variable-size signals can change their size during model execution in one of two ways:
At every step of model execution
Various blocks in the model modify the sizes of the signals during execution of the output method.
Only during initialization of conditionally executed subsystems
Size changes occur during distinct mode-switching events in subsystems such as action, enable, and function-call subsystems.
You can see the key difference by considering a Discrete 2-Tap Filter block with states.
Discrete 2-Tap Filter
Assume that the input signal dimension to this filter changes from 4 to 1 during simulation. It is ambiguous when and how the states of the Unit Delay blocks should adapt from 4 to 1 to continue processing the input. To ensure consistency, both Unit Delay blocks must change their state behavior synchronously. To prevent ambiguity, Simulink generally disallows blocks whose number of states depends on input signal sizes in contexts where signal sizes change at any point during execution.
In contrast, consider the same Discrete 2-Tap Filter block in a function-call subsystem. Assume that this subsystem is using the second way to propagate variable-size signals. In this case, the size of the input signal changes from 4 to 1 only at the initialization of the subsystem. At initialization, the subsystem resets all of its states (including the states of the two Unit Delay blocks) to their initial values. Resetting the subsystem ensures no ambiguity on the assignment of states to the input signal of the filter.
Mode-Dependent Variable-Size Signals shows how you can use the two ways of propagating variable-size signals in a complementary fashion to model complex systems.
Determine Whether Signal Line Has Variable Size
The following example demonstrates how to use commands at the command prompt or in a script to determine whether a signal line has a variable size. In a large model or hierarchy of subsystems or referenced models, use this technique to determine whether a signal has a variable size due to an upstream block.
The example model sldemo_varsize_basic
contains a signal
a
that is downstream of a Switch block.
Open the example model.
Select the Sum block whose output signal is labeled
a
.Type the following at the command window to set the model to a compiled state (similar to a diagram update).
sldemo_varsize_basic([],[],[],'compile')
Get a handle to the block output port.
portHandles = get_param(gcb,'portHandles'); outPortHandle = portHandles.Outport;
Query the programmatic parameter
CompiledPortDimensionsMode
of the output port.varSize = get_param(outPortHandle,'CompiledPortDimensionsMode')
varSize = 1
The value of the variable
varSize
is1
, which indicates that the signala
has variable size.The value
0
indicates that a signal does not have variable size.Terminate the model compilation.
sldemo_varsize_basic([],[],[],'term')
Empty Signals
An empty signal is a signal with a length of 0
. For example, signals
with size [0]
, [0x3]
, [2x0]
, and
[2x0x3]
are all empty signals. Simulink allows empty signals with variable-size signals and supports most element-wise
operations. However, Simulink does not support empty signals for blocks that modify signal dimensions.
Unsupported blocks include Reshape, Permute, and Sum along a specified dimension.
Simulink Block Support for Variable-Size Signals
The Simulink Block Data Type Support table includes a complete list of blocks that support variable-size signals.
To view the table:
Open a Simulink model.
In the MATLAB® command line, enter
showblockdatatypetable
.A separate window with the Simulink Block Data Type Support table opens.
An X
in the Variable-Size Support column
indicates support for that block.
Tip
You can also view the table by entering showblockdatatypetable
at the command prompt.
Subsystem Initialization of Variable-Size Signals
The initial signal size from an Outport block in a conditionally executed subsystem varies depending on the parameters you select.
If you set the Propagate sizes of variable-size signals parameter
in the parent subsystem to During execution
, the
Initial output parameter for the Output block must not exceed the
maximum size of the input port. If the Initial output parameter value
is:
Initial output parameter | Initial output signal size |
---|---|
A nonscalar matrix | The initial output signal size is the size of the Initial output parameter. |
A scalar | The initial output signal size is a scalar. |
The default [] | The initial output size is an empty signal (dimensions are all zeros). |
If you set the Propagate sizes of variable-size signals parameter
in the parent subsystem to Only when enabling
, the
Initial output parameter for the Output block must
be a scalar value.
When size is repropagated for the input of the Outport block, the initial output value is set using scalar expansion from the scalar parameter value.
If the Initial output parameter is the default value
[]
, Simulink treats the initial output as a grounded value.If the model does not activate the parent subsystem at start time (
t = 0
), the current size of the subsystem output corresponding to the Outport block is set to maximum size.When its parent subsystem repropagates signal sizes, the values of the subsystem variable-size output signals are also reset to their initial output parameter values.
Conditionally Executed Subsystem Blocks
Control port blocks are in conditionally executed subsystems. You can set the
Propagate sizes of variable-size signals parameter for these blocks
to During execution
, Only when execution is resumed
(Action Port), and Only when enabling
(Enable and Trigger or
Function-Call).
Trigger — Trigger type set to
function-call
Switching Blocks
Switching blocks support variable-size signals by allowing input signals with different sizes and propagating the size of the input signal to the output signal. You can set the Allow different data input sizes parameter for these blocks on the Signal Attributes pane to either on or off.
Variable-Size Signal Limitations
The following table describes variable-size signal limitations.
Limitation | Workaround |
---|---|
Array format logging does not support variable-size signals. | Use a Structure , Structure With
Time , or Dataset format for logging
variable-size signals. |
Variable-size signals must have a discrete sample time. | — |
Embedded Coder® does not support variable-size signals with ERT S-functions, custom storage classes, function prototype control, the AUTOSAR model interface, the C++ interface, and the ERT reusable code interface. | — |
Simulink does not support variable-size parameter or DWork vectors. | — |
Rapid accelerator mode does not support models having root-level input ports with variable-size signals. | — |
Virtual buses that you use as inputs to or outputs from a referenced model (Model block) do not support variable-size signals. | Configure the bus as nonvirtual. For more information about using buses as inputs to or outputs from a referenced model, see Use Buses at Model Interfaces. For more information about controlling bus virtuality, see Composite Interface Guidelines. |
Referenced models configured to use a local solver do not support variable-size signals on the model interface. | — |
Variable-size signals are not supported for blocks that specify an initial condition as a MATLAB structure. | — |
Variable-size signals are supported for arrays of buses only in normal simulation mode. | — |
Some simulation modes do not support signal logging for arrays of buses with variable-size signals. | Simulate the model in normal mode to log arrays of buses with variable-size signals using signal logging. (since R2023a) |
MATLAB Function block and Stateflow® chart do not support array of buses with bounded variable-size signals. Bounded variable-size signals indicate variable-size signals with known upper bounds. | — |
Some simulation modes do not support signal logging for nonvirtual buses with variable-size signals. | Simulate the model in normal mode or accelerator mode to log:
(since R2023a) |
Signal logging is not supported for virtual buses with variable-size signals that are not contained within a nested nonvirtual bus. | — |
You cannot apply a storage class to a root-level Outport block (see C Data Code Interface Configuration for Model Interface Elements (Simulink Coder)) if the signal that enters the block has a variable size. | Apply the storage class to the signal line instead of the Outport block. |