sf_varsize_example shows how MATLAB® functions
in Stateflow® charts exchange variable-size data with other charts
and blocks in the model. To open the model, click
In this model, one Stateflow chart,
uses temporal logic to generate a variable-size signal. A second chart,
computes the output based on the size of the signal generated by the
VarSizeSignalSource chart works like
a source block. It has no input and one variable-size output
For variable-size outputs, you must explicitly specify the size
and upper bound for each dimension. In this case,
a vector where the first dimension is assumed to be fixed at size
1 and the second dimension is variable with an upper bound of 16.
This chart uses temporal logic to transition between three states, each generating a different size output:
No states or transitions can read from or write to variable-size
y does not appear in any state
actions or transition logic. All computations involving variable-size
data must occur in MATLAB functions in the chart.
MATLAB functions access variable-size, chart-level data
directly. You do not pass the data as inputs or outputs to the functions.
In this chart, the
generateOutput function adds
a different number of elements to the variable-size output
based on how the active state calls it. The function constructs the
variable-size vector from a number sequence, then outputs the transpose
of the result:
function generateOutput(len) %#codegen assert(len<=16); y = (1:len)';
MATLAB functions must be able to determine the upper bounds
of variable-size data at compile time. In this case, however, the
upper bound is
len, an input for which the model
computes the value at run time. To provide this information, the
specifies an explicit upper bound for
that matches the upper bound for chart output
If you do not include the assert statement, you get a compilation error:
Computed maximum size is not bounded. Static memory allocation requires all sizes to be bounded. The computed size is [1 x :?].
size_based_processing chart computes
a variable-size output based on the value of a variable-size input:
u is the variable-size signal
generated by the
y is a variable-size signal
whose size depends on whether
u is a scalar or
The chart uses three MATLAB functions to evaluate the size
u and generate an associated output
As in the chart
data does not appear in state actions or transition logic. Instead,
states call MATLAB functions to compute the variable-size output.
Transitions call a MATLAB function in a conditional statement
to evaluate the variable-size input.
This function tests whether chart input
the signal generated by chart
is a scalar or vector value:
function isScalar = is_scalar_input %#codegen isScalar = length(u)==1;
u is a vector, this function outputs
the sine of each of its values:
function compute_output %#codegen y = sin(u);
u is a scalar, this function outputs
a value of zero:
function reset_output %#codegen y = 0;
Open the model:
Open the chart
but keep the Simulink® display blocks in view.
Start simulation from the chart.
The display blocks periodically show 1, 8, and 16 values from the variable-size vector.