Using Simulink Previous page   Next Page

Programming the Embedded MATLAB Function

You create a model with an Embedded MATLAB Function block in Creating an Example Embedded MATLAB Function. Now you want to add code to the block to define it as a function that takes a vector set of values and calculates the mean and standard deviation for those values. Use the following steps to program the function stats:

  1. If not already open, open the call_stats_block1 model that you save at the end of Adding an Embedded MATLAB Function Block to a Model, and double-click its Embedded MATLAB Function block fcn to open it for editing.
  1. The Embedded MATLAB Editor appears.

    The Embedded MATLAB Editor window is titled with the syntax <model name>/<Embedded MATLAB Function block name> in its header. In this example, the model name is call_stats_block1, and the block name is Embedded MATLAB Function, the name that appears at the bottom of the Embedded MATLAB Function block in Simulink.

    Inside the Embedded MATLAB Editor is an edit window for editing the function that specifies the Embedded MATLAB Function block. A function header with the function name fcn is at the top of the edit window. The header specifies an argument to the function, u, and a return value, y.

  1. Edit the function header line with the return values, function name, and argument as follows:
  1. The Embedded MATLAB function stats calculates a statistical mean and standard deviation for the values in the vector vals. The function header declares vals to be an argument to the stats function and mean and stdev to be return values from the function.

  1. In the Embedded MATLAB Editor, from the File menu, select Save As Model and save the model as call_stats_block2.
  1. Saving the model updates the Simulink window, which now has the following appearance:

    Changing the function header of the Embedded MATLAB Function block makes the following changes to the Embedded MATLAB Function block in the Simulink model:

  1. In the Simulink window, complete connections to the Embedded MATLAB Function block as shown.

  2. In the Embedded MATLAB Editor, enter a line space after the function header and replace the default comment line with the following comment lines:
  1. You specify comments with a leading percent (%) character, just as you do in MATLAB.

  1. Enter a line space after the comments and replace the default function line y = u; with the following:
  1. The function length is an example of a built-in function supported by the run-time function library for Embedded MATLAB Function blocks. This length works just like the MATLAB function length. It returns the vector length of its argument vals. However, when you simulate this model, Simulink generates C code for this function in the simulation application. Callable functions supported for Embedded MATLAB Function blocks are listed in the topic Embedded MATLAB Run-Time Function Library in Simulink documentation.

    The variable len is a local variable that is automatically typed as a scalar double because the Embedded MATLAB run-time library function, length, returns a scalar of type double. If you want, you can declare len to have a different type and size by changing the way you declare it in the function. See Declaring Local Variables Implicitly for details about implicitly declaring local variables in an Embedded MATLAB Function block.

    By default, implicitly declared local variables like len are temporary. They come into existence only when the function is called and cease to exist when the function is exited. You can make implicitly declared variables for a function persistent between calls by using the persistent statement.

  1. Enter the following lines to calculate the value of mean and stdev:
  1. stats stores the mean and standard deviation values for the values in vals in the variable mean and stdev, which are output by port to the Display blocks in the Simulink model. The line that calculates mean calls a subfunction, avg, that has not been defined yet. The line that calculates stdev calls the Embedded MATLAB run-time library functions sqrt and sum.

  1. Enter the following line to plot the input values in vals.
  1. This line calls the function plot to plot the input values sent to stats against their vector indices. 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 for more details on using this mechanism to call MATLAB functions from Embedded MATLAB functions.

  1. Enter a line space followed by the following lines for the subfunction avg, which is called in an earlier line.
  1. These two lines define the subfunction avg. You are free to use subfunctions in Embedded MATLAB function code with single or multiple return values, just as you do in regular MATLAB functions.

    The Embedded MATLAB Editor should now have the following appearance:

  1. Save the model again as call_stats_block2.

Previous page  Adding an Embedded MATLAB Function Block to a Model Checking the Function for Errors Next page

© 1994-2005 The MathWorks, Inc.