Compute Output Based on Size of Input Signal

About the Model

The model 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 sf_varsize_example.

In this model, one Stateflow chart, VarSizeSignalSource, uses temporal logic to generate a variable-size signal. A second chart, size_based_processing, computes the output based on the size of the signal generated by the first chart:

Chart: VarSizeSignalSource

The VarSizeSignalSource chart works like a source block. It has no input and one variable-size output y:

For variable-size outputs, you must explicitly specify the size and upper bound for each dimension. In this case, y is 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:

How the Chart Works with the Variable-Size Output

No states or transitions can read from or write to variable-size data. Therefore, 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.

How the MATLAB Function Works with the Variable-Size Output

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 y, 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)
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 assert function specifies an explicit upper bound for len, one that matches the upper bound for chart output y.

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 :?].

Chart: size_based_processing

The size_based_processing chart computes a variable-size output based on the value of a variable-size input:

  • Input u is the variable-size signal generated by the VarSizeSignalSource chart:

  • Output y is a variable-size signal whose size depends on whether u is a scalar or vector:

    The chart uses three MATLAB functions to evaluate the size of input u and generate an associated output y:

    As in the chart VarSizeSignalSource, variable-size 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.

MATLAB Function: is_scalar_input

This function tests whether chart input u, the signal generated by chart VarSizeSignalSource, is a scalar or vector value:

function isScalar = is_scalar_input
isScalar = length(u)==1;

MATLAB Function: compute_input

If input u is a vector, this function outputs the sine of each of its values:

function compute_output
y = sin(u);

MATLAB Function: reset_output

If input u is a scalar, this function outputs a value of zero:

function reset_output
y = 0;

Simulate the Model

  1. Open the model:

  2. Open the chart VarSizeSignalSource, but keep the Simulink® display blocks in view.

  3. Start simulation from the chart.

    The display blocks periodically show 1, 8, and 16 values from the variable-size vector.

Was this topic helpful?