Main Content

Code Generation for Nested and Anonymous MATLAB Functions

R2026b

You can generate code for nested and anonymous MATLAB® functions. However, because MATLAB handles function scope and workspace access differently from C or C++, certain restrictions apply.

Anonymous Function Limitations

For code generation, anonymous functions have the same limitations as value classes and cell arrays. See Class Limitations for Code Generation and Cell Array Limitations for Code Generation.

In addition, you cannot use anonymous functions to represent Simulink® signals, parameters, or data store memory.

Nested Function Limitations

When you generate code for nested functions, you must adhere to the code generation restrictions for value classes, handle classes, and cell arrays. See Class Limitations for Code Generation and Cell Array Limitations for Code Generation.

In addition:

  • If the parent function declares a persistent variable, it must assign the variable before calling a nested function that uses the persistent variable.

  • A nested recursive function cannot refer to a variable that the parent function uses.

  • If a nested function refers to a structure variable, you must define the structure by using the struct function.

  • If a nested function uses a variable defined by the parent function, you cannot call coder.varsize on the variable in either the parent or the nested function.

  • Sharing a variable-size variable between a nested function and its parent function requires dynamic memory allocation. If you disable dynamic memory allocation, explicitly pass the shared variable to the nested function as an input argument. For more information about dynamic memory allocation, see Control Dynamic Memory Allocation in Generated Code.

See Also

| |

Topics