| Stateflow® | ![]() |
This section explains how to create a Simulink model with a Stateflow block that calls two Embedded MATLAB functions, meanstats and stdevstats. meanstats calculates a mean and stdevstats calculates a standard deviation for the values in vals and outputs them to the Stateflow data mean and stdev, respectively.
Follow these steps:
Create a new Simulink model with the following blocks:

In the Simulink model, double-click the Stateflow block to open the Stateflow Editor.
In the Stateflow Editor, drag two Embedded MATLAB functions into the empty Stateflow chart using this icon from the tool palette:
![]()
A text field with a flashing cursor appears in the middle of each Embedded MATLAB function.

You must label an Embedded MATLAB function with its signature. Use the following syntax:
[return_val1, return_val2,...] = function_name(arg1, arg2,...)
You can specify multiple return values and multiple input arguments, as shown in the syntax. Each return value and input argument can be a scalar, vector, or matrix of values.
In the Stateflow chart, draw a default transition into a terminating junction with this condition action:
{mean = meanstats(invals);
stdev = stdevstats(invals);}
The Stateflow chart should look like this figure.

In the Stateflow chart, double-click the function meanstats to edit its function body in the Embedded MATLAB Editor.
In the Embedded MATLAB Editor, select Tools > Model Explorer.
The Model Explorer appears.

The function meanstats is highlighted in the Model Hierarchy pane. The Contents pane displays the input argument vals and output argument meanout. Both are scalars of type double by default.
Double-click the vals row under the Size column to set the size of vals to 4.
Back in the Stateflow chart, double-click the function stdevstats and repeat steps 8 and 9.
Back in the Model Hierarchy pane of the Model Explorer, select Chart and add the following data:
Name | Scope | Size |
|---|---|---|
invals | Input | 4 |
mean | Output | Scalar (no change) |
stdev | Output | Scalar (no change) |
You should now see the following data in the Model Explorer.

After you add the data invals, mean, and stdev to the Stateflow chart, the corresponding input and output ports appear on the Stateflow block in the Simulink model.

Connect the Constant block and the Display block to the ports of the Stateflow block and save the model.

The section Debugging an Embedded MATLAB Function shows you how to program the functions meanstats and stdevstats.
![]() | Introduction to Embedded MATLAB Functions | Programming an Embedded MATLAB Function | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |