Class Limitations for Code Generation
R2026bYou can generate code for MATLAB® value classes, handle classes, and System objects. By default, MATLAB classes are value classes. To create a handle class, you must derive the class
from the handle superclass. A System object™ is a type of handle class that derives from the matlab.System class. To learn more about value and handle classes in
MATLAB, see Comparison of Handle and Value Classes. To learn more about System objects, see What Are System Objects?.
The limitations described here apply when you generate code for an entry-point function that uses a MATLAB class. You must also be aware of considerations that apply when you define class properties for code generation. See Define Class Properties for Code Generation.
Limitations for Value and Handle Classes
When you use a MATLAB class in code intended for code generation:
You cannot generate code for object arrays. See Resolve Error: Code Generation Does Not Support String and Object Arrays.
A constructor method cannot have multiple outputs.
A constructor method cannot have nested functions.
You cannot use classes for Simulink® signals, parameters, or data store memory.
You cannot declare a class or a method as extrinsic by using
coder.extrinsic.You cannot pass a class or an instance of a class to
coder.ceval. However, you can usecoder.cevalwith class properties.When you use a class in a MATLAB Function block, you cannot use the debugger to view class information.
You cannot generate code for a class that overloads the operators
subsref,subsasgn, orsubsindex.If a subclass derives from a MATLAB built-in type, the subclass must be an enumeration class. See Code Generation for Enumerations.
Additional considerations apply when you define the properties of a MATLAB class in code intended for code generation. See Define Class Properties for Code Generation.
Usage Notes and Limitations for Handle Classes
The code generator statically determines the lifetime of a handle object. By performing static analysis, the generated code can reuse memory rather than rely on a dynamic memory management scheme, such as reference counting or garbage collection. In this way, the code generator can avoid dynamic memory allocation and run-time automatic memory management. These generated code characteristics are important for some safety-critical and real-time applications.
However, the static analysis performed by the code generator means that these additional limitations apply when you use handle classes in MATLAB code for code generation:
A handle class object cannot be an entry-point function input or output.
A handle class cannot be contained in class, cell, or structure that is an entry-point function input or output. For example, if a value class object contains a handle class object, then the value class object cannot be an entry-point function input or output.
Global variables cannot be handle classes or handle class objects.
The code generator does not produce upper bounds for variable-size matrices in handle class. Inside handle classes, variable-size matrix dimensions are treated as unbounded, even if you have specified an upper bound. If you disable dynamic memory allocation and you use an upper-bounded variable-size matrix in a handle class, code generation fails. See Control Dynamic Memory Allocation in Generated Code.
Events and listeners are not supported.
Recursive data structures, such as linked lists, trees, and graphs, are not supported.
The
AbortSetproperty attribute is not supported.For additional considerations that apply when you generate code for a System object, see in System Objects in MATLAB Code Generation.
See Also
codegen | coder.ClassType | handle | matlab.System