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
Create a new Simulink model.
Add these blocks to your model:
Chart block
Constant block
Two Display blocks
Save the model as
call_stats_function_stateflow
.
Configure the Stateflow Chart
Open the Chart block.
Add two MATLAB functions by using the MATLAB Function icon
.
Label the functions with these signatures:
meanout = meanstats(vals)
stdevout = stdevstats(vals)
Add a default transition to a terminating junction with this condition action:
{ mean = meanstats(invals); stdev = stdevstats(invals); }
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
Open the Symbols pane.
Set the data types for these data by using the Type column::
invals
: Input Datastdev
: Output Datamean
: Output Data
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.
Click Run. The mean and standard deviation numbers appear in the Display block window.
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
coder.extrinsic
(Simulink)