| Contents | Index |
The following model contains a Stateflow chart with a MATLAB function.

The chart contains the following logic:

The function contains the following code:
function stats(vals)
%#codegen
% calculates a statistical mean and standard deviation
% for the values in vals.
len = length(vals);
mean = avg(vals, len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
coder.extrinsic('plot');
plot(vals,'-+');
function mean = avg(array,size)
mean = sum(array)/size;You will build a similar model in Building a Model with a MATLAB Function in a Chart.
Note in this example that the MATLAB function can call any of these types of functions:
Subfunctions
Subfunctions are defined in the body of the MATLAB function. In this example, avg is a subfunction. See Calling Subfunctions in the Code Generation from MATLAB documentation.
Stateflow functions
Graphical, truth table, and other MATLAB functions can be called from a MATLAB function in a chart.
MATLAB toolbox functions that support code generation
Toolbox functions for code generation are a subset of the functions that you can call in the MATLAB workspace. These functions generate C code for building targets that conform to the memory and data type requirements of embedded environments. In this example, length, sqrt, and sum are examples of toolbox functions for code generation. See Calling Supported Toolbox Functions in the Code Generation from MATLAB documentation.
MATLAB toolbox functions that do not support code generation
You can also call extrinsic functions on the MATLAB path that do not generate code. These functions execute only in the MATLAB workspace during simulation of the model. See Calling MATLAB Functions in the Code Generation from MATLAB documentation.
Simulink Design Verifier functions
Simulink Design Verifier software provides MATLAB functions for property proving and test generation.
![]() | Where to Use a MATLAB Function | Building a Model with a MATLAB Function in a Chart | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |