| Stateflow User's Guide | ![]() |
Building a Simulink Model with a Stateflow Embedded MATLAB Function
Embedded MATLAB functions are special Stateflow functions that use the Embedded MATLAB Language, a rich subset of the MATLAB programming language. This section takes you through the steps of creating a Simulink model with a Stateflow block that calls an Embedded MATLAB function, stats. stats calculates a mean and a standard deviation for the values in vals and stores them in the Stateflow data mean and stdev, respectively. Use the following steps to create a model with a Stateflow diagram that calls an Embedded MATLAB function. In the process, learn how to use embedded MATLAB functions in Stateflow.
call_stats_function.
stats(vals) for the new Embedded MATLAB function, as shown.
Like all Stateflow functions, the signature of an Embedded MATLAB function is entered in its label with the following syntax:
Each argument and the return value can be a scalar, vector, or matrix of values. A matrix is a two-dimensional array of values. A vector is a matrix with a row or column dimension of 1. Multiple return values are not allowed.
stats to edit its function body.
Notice that the function stats is highlighted in the Model Hierarchy pane (left). The Contents pane (right) displays the argument data vals, a scalar of type double with a scope of Function Input that was added when you labeled the Embedded MATLAB function.
vals is defined for an Embedded MATLAB function, its first index, which is not displayed, is 1 by default, as in MATLAB arrays.
| Name |
Scope |
Size |
invals |
Input from Simulink |
4 |
mean |
Output to Simulink |
Scalar (no change) |
stdev |
Output to Simulink |
Scalar (no change) |
Now that you have added the data invals, mean, and stdev to the Stateflow diagram Chart, notice that an input port for invals and output ports for mean and stdev appear on the Stateflow block in Simulink.
Notice in the Model Explorer that the value of Port for mean is 1 and for stdev is 2. The value of Port orders the ports from the top to the bottom of the block for both input and output data. You can change the order of the ports by entering a new value for the Port field.
call_stats_function).
| Introduction to Embedded MATLAB Functions | Programming a Stateflow Embedded MATLAB Function | ![]() |
© 1994-2005 The MathWorks, Inc.