| Contents | Index |
To program the functions meanstats and stdevstats that you created in Building a Model with a MATLAB Function in a Chart, follow these steps:
In the chart, open the function meanstats.
The function editor appears with the header:
function meanout = meanstats(vals)
This header is taken from the function label in the chart. You can edit the header directly in the editor, and your changes appear in the chart after you close the editor.
On the line after the function header, enter:
%#codegen
The %#codegen compilation directive helps detect compile-time violations of syntax and semantics in MATLAB functions supported for code generation. To learn more about detecting compile-time errors, see Adding the Compilation Directive %#codegen in the Code Generation from MATLAB documentation.
Enter a line space and this comment:
% Calculates the statistical mean for vals
len = length(vals);
The function length is an example of a built-in MATLAB function that is supported for code generation. You can call this function directly to return the vector length of its argument vals. When you build a simulation target, the function length is implemented with generated C code. Functions supported for code generation appear in Functions Supported for Code Generation in the Code Generation from MATLAB documentation.
The variable len is an example of implicitly declared local data. It has the same size and type as the value assigned to it — the value returned by the function length, a scalar double. To learn more about declaring variables, see Defining MATLAB Variables for C/C++ Code Generation in the Code Generation from MATLAB documentation.
The MATLAB function treats implicitly declared local data as temporary data, which exists only when the function is called and disappears when the function exits. You can declare local data for a MATLAB function in a chart to be persistent by using the persistent construct (see Defining and Initializing Persistent Variables in the Code Generation from MATLAB documentation).
Enter this line to calculate the value of meanout:
meanout = avg(vals,len);
The function meanstats stores the mean of vals in the Stateflow data meanout. Because these data are defined for the parent Stateflow chart, you can use them directly in the MATLAB function.
Two-dimensional arrays with a single row or column of elements are treated as vectors or matrices in MATLAB functions. For example, in meanstats, 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 MATLAB function uses the functions avg and sum to compute the value of mean. sum is a function supported for code generation. avg is a subfunction that you define later. When resolving function names, MATLAB functions in your chart look for subfunctions first, followed by functions supported for code generation.
Note If you call a function that the MATLAB function cannot resolve as a subfunction or a function for code generation, you must declare the function to be extrinsic. For more information, see Calling MATLAB Functions in the Code Generation from MATLAB documentation. |
Now enter this statement:
coder.extrinsic('plot');Enter this line to plot the input values in vals against their vector index.
plot(vals,'-+');
Recall that you declared plot to be an extrinsic function because it is not supported for code generation. When the MATLAB function encounters an extrinsic function, it sends the call to the MATLAB workspace for execution during simulation.
Now, define the subfunction avg, as follows:
function mean = avg(array,size) mean = sum(array)/size;
The header for avg defines two arguments, array and size, and a single return value, mean. The subfunction avg calculates the average of the elements in array by dividing their sum by the value of argument size.
For more information on creating subfunctions, see Subfunctions in the MATLAB documentation.
The complete code for the function meanstats looks like this:
function meanout = meanstats(vals)
%#codegen
% Calculates the statistical mean for vals
len = length(vals);
meanout = avg(vals,len);
coder.extrinsic('plot');
plot(vals,'-+');
function mean = avg(array,size)
mean = sum(array)/size;
Back in the chart, open the function stdevstats and add code to compute the standard deviation of the values in vals. The complete code should look like this:
function stdevout = stdevstats(vals) %#codegen % Calculates the standard deviation for vals len = length(vals); stdevout = sqrt(sum(((vals-avg(vals,len)).^2))/len); function mean = avg(array,size) mean = sum(array)/size;
![]() | Building a Model with a MATLAB Function in a Chart | Debugging 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 |