| Embedded MATLAB™ | ![]() |
| On this page… |
|---|
Using Function Handles with the Embedded MATLAB Subset Example: Defining and Passing Function Handles in an Embedded MATLAB Function |
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:
Define handles that reference user-defined functions and built-in functions supported by the Embedded MATLAB subset (see Embedded MATLAB Function Library Reference)
Note You cannot define handles that reference extrinsic MATLAB functions (see Calling MATLAB Functions). |
Define function handles as scalar values
Pass function handles as arguments to other functions (excluding extrinsic functions)
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
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:
At the MATLAB command prompt, issue this command:
emlmex addval
Embedded MATLAB MEX checks your code for compliance with the Embedded MATLAB subset.
Define and initialize a 3-by-3 matrix by typing a command like this at the MATLAB prompt:
m = zeros(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 3For more information, see Working with Embedded MATLAB MEX.
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
![]() | Using Structures | Specifying Variable Numbers of Arguments in Embedded MATLAB Functions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |