Main Content

Integrate MATLAB Functions in Stateflow Charts

To implement complex algorithms in your Stateflow® chart, use MATLAB® functions. MATLAB functions allow you to combine mathematical capabilities of MATLAB in your Stateflow chart. For more information about creating MATLAB functions, see Reuse MATLAB Code by Defining MATLAB Functions.

Supported Function Types

Stateflow charts can call these MATLAB function types:

  • Local functions that you define in the function body.

  • Graphical, Simulink®, and truth table functions.

  • Built-in MATLAB functions that support code generation.

  • Extrinsic functions for simulation-only execution. For more information, see Call Extrinsic MATLAB Functions in Stateflow Charts.

Add MATLAB Functions to a Stateflow Chart

In this example, you build a model that contains two MATLAB functions and then use the functions to calculate the mean and standard deviation of data that you input to the chart.

Set Up the Model

  1. Create a new Simulink model.

  2. Add these blocks to your model:

    • Chart block

    • Constant block

    • Two Display blocks

  3. Simulink model that contains a Stateflow chart, a constant block, and two display blocks.

  4. Save the model as call_stats_function_stateflow.

Configure the Stateflow Chart

  1. Open the Chart block.

  2. Add two MATLAB functions by using the MATLAB Function icon .

  3. Label the functions with these signatures:

    • meanout = meanstats(vals)

    • stdevout = stdevstats(vals)

      Stateflow chart with two MATLAB functions called meanstats and stdevstats.

  4. Add a default transition to a terminating junction with this condition action:

    {
    mean = meanstats(invals);
    stdev = stdevstats(invals);
    }

    Stateflow chart with a transition that calls the two MATLAB functions.

    If the arguments of a function signature are scalars, check that the inputs and outputs of the function calls follow the rules of scalar expansion. For more information, see Assign Values to All Elements of a Matrix.

Define Data Elements

  1. Open the Symbols pane.

  2. Set the data types for these data by using the Type column::

    • invals: Input Data

    • stdev: Output Data

    • mean: Output Data

    The data in the symbols pane.

Program the Standard Deviation Function

Open the stdevstats function and add this code:

function stdevout = stdevstats(vals)
%#codegen

% Calculate standard deviation
len = length(vals);
stdevout = sqrt(sum(((vals-avg(vals,len)).^2))/len);

function meanfcn = avg(inputArray,inputSize)
    meanfcn = sum(inputArray)/inputSize;

The function sum computes the value of stdevout. The %#codegen compilation directive helps detect compile-time violations of syntax and semantics in MATLAB functions supported for code generation.

Program the Mean Function

Open the meanstats function and add this code:

function meanout = meanstats(vals)
%#codegen

% Calculate statistical mean
len = length(vals);
meanout = avg(vals,len);

% Plot results (simulation only)
coder.extrinsic("plot");
plot(vals,"-+");

function meanfcn = avg(inputArray,inputSize)
    meanfcn = sum(inputArray)/inputSize;

The length function supports code generation, and returns the length of a vector. The function avg computes the value of meanout. You define plot as extrinsic, because it is not supported for code generation.

Simulate the Model

To simulate the model, first connect the Simulink blocks to the chart input and output ports.

The Simulink model is connected to Stateflow.

Click Run. The mean and standard deviation numbers appear in the Display block window.

The Display blocks show the results of simulation.

Code Generation Requirements

To generate code, all functions must support code generation. If a function does not support code generation, you can use coder.extrinsic (Simulink) to mark it as an exception. For a list of functions that MATLAB supports for code generation, see Functions and Objects Supported for C/C++ Code Generation (MATLAB Coder).

See Also

(Simulink)

Topics