Code Generation for Anonymous Functions

You can use anonymous functions in MATLAB® code intended for code generation. For example, you can generate code for the following MATLAB code that defines an anonymous function that finds the square of a number.

sqr = @(x) x.^2;
a = sqr(5);

Anonymous functions are useful for creating a function handle to pass to a MATLAB function that evaluates an expression over a range of values. For example, this MATLAB code uses an anonymous function to create the input to the fzero function:

b = 2;
c = 3.5;
x = fzero(@(x) x^3 + b*x + c,0);

Anonymous Function Limitations for Code Generation

The data type of an anonymous function is function_handle. Therefore, for code generation, anonymous functions have the same limitations that function handles have. Anonymous functions also have the code generation limitations of handle classes.

More About

Was this topic helpful?