Programming an Embedded MATLAB Function

To program the functions meanstats and stdevstats that you created in Building a Simulink Model with an Embedded MATLAB Function, follow these steps:

  1. Open the Stateflow chart in the model call_stats_function_stateflow.

  2. In the Stateflow chart, open the Embedded MATLAB function meanstats.

    The Embedded MATLAB Editor appears with the function header as shown.

    This function header is taken from the label that you added to the function in the Stateflow chart. You can edit it directly in the Embedded MATLAB Editor, and your changes will be reflected in the Stateflow Editor when you close the window or click the Update Diagram icon in the toolbar:

  3. After the function header, enter a line space and this comment:

    % Calculates a statistical mean for vals
    
  4. Now enter this statement:

    eml.extrinsic('plot');

    The function plot is a MATLAB function that is not supported by the Embedded MATLAB subset. To call unsupported MATLAB functions, you must first declare them to be extrinsic, as described in Calling MATLAB Functions in the Embedded MATLAB User's Guide.

  5. Add the line:

    len = length(vals);
    

    The function length is an example of a built-in MATLAB function that is supported by the Embedded MATLAB subset. 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. MATLAB functions supported by the Embedded MATLAB subset appear in Embedded MATLAB Function Library Reference in the Embedded MATLAB User's Guide.

    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. You can change the size and type of len as described in in the Embedded MATLAB User's Guide.

    The Embedded MATLAB function treats implicitly declared local data as temporary data. It comes into existence only when the function is called and disappears when the function exits. You can declare local data for an Embedded MATLAB function to be persistent by using the persistent construct (see Declaring Persistent Variables in the Embedded MATLAB User's Guide).

  6. 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 . Since these data are defined for the parent Stateflow chart, you can use them directly in the Embedded MATLAB function.

    Two-dimensional arrays with a single row or column of elements are treated as vectors or matrices in Embedded 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 Embedded MATLAB function uses the functions avg and sum to compute the value of mean. sum is an Embedded MATLAB run-time library function. avg is a subfunction that you define later. When resolving function names, Embedded MATLAB functions look for subfunctions first, followed by Embedded MATLAB run-time library functions.

  7. 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 in the Embedded MATLAB runtime library. When the Embedded MATLAB function encounters an extrinsic function, it sends the call to the MATLAB workspace for execution during simulation.

  8. 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 MATLAB software documentation.

    The complete code for the Embedded MATLAB function meanstats looks like this:

    function meanout = meanstats(vals)
    
    % Calculates a statistical mean for vals
    
    eml.extrinsic('plot');
    len = length(vals);
    meanout = avg(vals,len);
    
    plot(vals,'-+');
    
    function mean = avg(array,size)
    mean = sum(array)/size;
    
  9. Save the model (call_stats_function_stateflow).

  10. Back in the Stateflow chart, open the second Embedded MATLAB 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)
    
    %Calculate 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;

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS