Main Content

Integrate MATLAB Functions in Stateflow Charts

You can use MATLAB® functions to implement complex algorithms in your Stateflow® charts. You can use MATLAB functions to:

  • Express mathematical calculations, proccess data, or analyze statistics.

  • Incorporate established MATLAB algorithms into your state machine.

  • Encapsulate complex logic to make the chart design cleaner.

For more information about creating MATLAB functions for use in Stateflow charts, see Reuse MATLAB Code by Defining MATLAB Functions.

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 a Chart block, Constant block, and two Display blocks.

  3. Set the value of the Constant block to [2 3 4 5].

    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.

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

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

The Display blocks show the results of simulation.

Generate Code from Charts That Use MATLAB Functions

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