Compute Output Based on Size of Input Signal

This example shows how to modify the size of output data in a Stateflow® chart during run time. Stateflow charts exchange variable-size data with other charts and blocks in the model through MATLAB® functions, Simulink® functions, and MATLAB truth tables. This example illustrates this process by using MATLAB functions in two Stateflow charts.

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

VarSizeSignalSource Chart

The VarSizeSignalSource chart uses temporal logic to transition between three states. Each state generates a different size output.

The 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 whose length has an upper bound of 16.

Because states or transitions cannot read from or write to variable-size data, 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 Function: generateOutput

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 constructs the variable-size output vector y as a number sequence based on the input it receives.

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

SizeBasedProcessing Chart

The SizeBasedProcessing chart uses three MATLAB functions to compute a variable-size output y based on the value of a variable-size input u.

  • 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.

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: isScalarInput

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

function isScalar = isScalarInput
%#codegen
isScalar = length(u)==1;

MATLAB Function: computeOutput

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

function computeOutput
%#codegen
y = sin(u);

MATLAB Function: resetOutput

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

function resetOutput
%#codegen
y = 0;

Simulate the Model

  1. Open the model. The tabs along the top of the Editor canvas enable you to switch between the Simulink model and the two Stateflow charts.

  2. Start simulation from the VarSizeSignalSource chart. The chart animation shows the active state cycling between the Scalar, VectorPartial, and VectorFull states.

  3. Change tabs to view the SizeBasedProcessing chart. The chart animation shows the active state alternating between the Scalar and Vector states.

  4. Change tabs to return to the Simulink model. The display blocks periodically show 1, 8, and 16 values from the variable-size vectors.

Related Topics