Main Content

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

Anonymous functions have the code generation limitations of value classes and cell arrays.

You can use anonymous functions in a MATLAB Function block. You cannot use anonymous functions for Simulink® signals, parameters, or data store memory.

Related Topics