| Stateflow User's Guide | ![]() |
Programming a Stateflow Embedded MATLAB Function
You program an Embedded MATLAB function with the Embedded MATLAB language, a rich subset of MATLAB. Specify the contents of the Embedded MATLAB function stats that you create in Building a Simulink Model with a Stateflow Embedded MATLAB Function with the following steps:
call_stats_function model that you save at the end of Building a Simulink Model with a Stateflow Embedded MATLAB Function, and open its Stateflow block and the Embedded MATLAB function stats inside.
stats to open it for editing.
stats as shown.
The Embedded MATLAB Editor is titled with the syntax <model name>/<Embedded MATLAB function name>. In this case, the model name is call_stats_function, and the function name is Chart.stats, where Chart is the name of the owning Stateflow block in Simulink.
The first line of the function, the function header, is already present:
This function header is taken from the label that you add to the function in the Stateflow diagram. You can edit it directly in the Embedded MATLAB Editor. Changing the function header in the editor changes the Embedded MATLAB function label in the Stateflow diagram editor when you close the window or select the Update Diagram tool
in the editor toolbar.
length is an example of a built-in function supported by the Embedded MATLAB run-time function library. This length function works just like the MATLAB function length, and returns the vector length of its argument vals. However, when you build a simulation target for this function, the function length is implemented with generated C code. Callable functions supported for Embedded MATLAB blocks are listed in the topic "Using the Run-Time Library of Functions."
The variable len is an example of implicitly declared local data. Because the Embedded MATLAB run-time library function length returns a scalar double, len is scalar data of type double. You can declare len to have a different type and size by changing the way you declare it. See Declaring Local Variables Implicitly in Simulink documentation for a description and examples.
Implicitly declared local data is temporary data. It comes into existence only when the function is called and disappears when the function is exited. You can declare local data for an Embedded MATLAB function to be persistent by using persistent. You can also declare local data for an Embedded MATLAB function that is persistent by declaring it in the Model Explorer.
stats stores the mean and standard deviation for the values in vals in the Stateflow data mean and stdev. Since these data are defined for the chart, which is the parent of stats, you can use them directly in the Embedded MATLAB function. You can use Stateflow data of all types and sizes that are in the scope of an Embedded MATLAB function directly, except for data of type ml, which is not supported in Embedded MATLAB functions.
Two-dimensional arrays with a single row or column of elements are treated as vectors or as matrices in Embedded MATLAB functions. For example, in the Embedded MATLAB function stats, the argument vals is a four element vector. You can access the fourth element of this vector with the matrix notation vals(4,1) or the vector notation vals(4).
The preceding line uses the functions avg, sqrt, and sum to compute the values of mean and stdev. sqrt and sum are Embedded MATLAB run-time library functions. avg is a subfunction of the Embedded MATLAB function stats that you define in the next step. When resolving function names, Embedded MATLAB functions look for subfunctions first, followed by Embedded MATLAB run-time library functions.
| Note For simulation targets, if a called function in an Embedded MATLAB function cannot be resolved as a subfunction or Embedded MATLAB run-time library function, it is resolved as a MATLAB function, which is executed in the MATLAB workspace by the simulation application during simulation. See Calling MATLAB Functions in Simulink documentation for a description of this process and an example. |
plot to plot the input values sent to stats against their vector index. Because the Embedded MATLAB run-time library has no plot function, the Embedded MATLAB function cannot resolve this call with a subfunction or an Embedded MATLAB run-time function. Instead, it replaces this call with a call to the MATLAB plot function in the generated code for the simulation target.
See Calling MATLAB Functions in documentation for more details on using this mechanism to call MATLAB functions from Embedded MATLAB functions.
avg at the end of the top-level function stats. The header for avg defines two arguments, array and size, and a single return value, mean. The only line of programming in the subfunction avg calculates the average of the elements of the array argument by summing them with a call to the Embedded MATLAB library function sum and dividing the result with the argument size.
Embedded MATLAB functions support subfunctions with multiple return values. As in MATLAB, separate the return values by commas or spaces and surround the list with square brackets. For more information on creating subfunctions, see Subfunctions in MATLAB online documentation.
The finished Embedded MATLAB function stats has the following appearance:
| Building a Simulink Model with a Stateflow Embedded MATLAB Function | Debugging a Stateflow Embedded MATLAB Function | ![]() |
© 1994-2005 The MathWorks, Inc.