Working with Function Handles

Using Function Handles with the Embedded MATLAB Subset

You can use function handles in the Embedded MATLAB subset to invoke functions indirectly and parameterize operations that you repeat frequently (see Function Handles in the MATLAB Programming Fundamentals documentation). You can perform the following operations with function handles:

The Embedded MATLAB subset does not support the full set the operations you can perform with function handles in MATLAB, as described in Limitations with Function Handles

Example: Defining and Passing Function Handles in an Embedded MATLAB Function

The following code example defines and calls function handles in an Embedded MATLAB function. You can copy it as is to an Embedded MATLAB Function block in Simulink or Embedded MATLAB function in Stateflow. To convert this function to a C-MEX function using emlmex, uncomment the two calls to the assert function, highlighted below:

function addval(m)

  
  % Declare class and size of primary input m
  % Uncomment next two lines to build C-MEX function with emlmex
  % assert(isa(m,'double'));
  % assert(all (size(m) == [3 3]));

  % Declare MATLAB function disp to be extrinsic
  eml.extrinsic('disp');

  disp(m);

  % Pass function handle to addone
  %  to add one to each element of m
  m = map(@addone, m);
  disp(m);

  % Pass function handle to addtwo
  %  to add two to each element of m
  m = map(@addtwo, m);
  disp(m);
  
  function y = map(f,m)
    y = m;
    for i = 1:numel(y)
       y(i) = f(y(i));
    end

  function y = addone(u)
  y = u + 1;
  
  function y = addtwo(u)
  y = u + 2;

This code passes function handles @addone and @addtwo to the function map which increments each element of the matrix m by the amount prescribed by the referenced function. Note that map stores the function handle in the input variable f and then uses f to invoke the function — in this case addone first and then addtwo.

If you have Simulink or Fixed-Point Toolbox, you can use Embedded MATLAB MEX to convert this M-function addval to a C-MEX executable that you can run in MATLAB. Follow these steps:

  1. At the MATLAB command prompt, issue this command:

    emlmex addval

    Embedded MATLAB MEX checks your code for compliance with the Embedded MATLAB subset.

  2. Define and initialize a 3-by-3 matrix by typing a command like this at the MATLAB prompt:

    m = zeros(3)
  3. Execute the function by typing this command:

    addvals(m)

    You should see the following result:

         0     0     0
         0     0     0
         0     0     0
    
         1     1     1
         1     1     1
         1     1     1
    
         3     3     3
         3     3     3
         3     3     3

For more information, see Working with Embedded MATLAB MEX.

Limitations with Function Handles

The Embedded MATLAB subset supports MATLAB function handles with the following limitations:

 Function handles must be scalar values.

 You cannot use the same bound variable to reference different function handles.

 You cannot pass function handles to or from extrinsic functions.

 You cannot pass function handles as inputs to primary functions.

 You cannot view function handles from the debugger

  


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