MATLAB Classes
You can generate code for MATLAB functions that use value classes, handle classes, and System objects. 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?.
In MATLAB code for code generation, you can:
Use a MATLAB value or handle class in the MATLAB code.
Use a MATLAB value class as an input argument to a MATLAB entry point.
If you generate C code, MATLAB classes are represented as structures. If you generate C++ code, the default behavior of the code generator is to produce C++ classes for the MATLAB classes. You must be aware of certain usage notes and limitations when using classes in your MATLAB code that is intended for code generation.
Classes
coder.ClassType | Represent set of MATLAB classes acceptable for input specification |
Objects
coder.ClassSignature | Entry-point class for code generation (Tech Preview) (Since R2026a) |
coder.FunctionSignature | Entry-point function or method for code generation (Tech Preview) (Since R2026a) |
Functions
addMethod | Add method specification to coder.ClassSignature object (Tech
Preview) (Since R2026a) |
Topics
Code Generation Basics
- Class Limitations for Code Generation
Adhere to code generation restrictions when you use classes in MATLAB code for code generation. - Define Class Properties for Code Generation
Define the properties of value and handle classes in MATLAB code for code generation. - System Objects in MATLAB Code Generation
Special considerations for using System objects in code generated from MATLAB. - Code Generation for Handle Class Destructors
Use a handle class destructor in MATLAB code intended for code generation. - Generate C++ Code for MATLAB Classes
Generate a C++ class for a value class, handle class, or System object™ in your MATLAB code. - Generate Class Interface in C++ Code
Generate C++ code that is packaged into a class.
Use MATLAB Class as Entry Point (Tech Preview)
- Generate Standalone Code for Entry-Point Class (Tech Preview)
Use a MATLAB class as an entry-point for code generation. (Since R2026a) - Generate C++ Executable for Entry-Point System Object in a Namespace (Tech Preview)
Use a System object in a namespace as an entry point for code generation. (Since R2026a) - Generate and Use C++ Classes from Entry-Point Classes That Model a Physical System (Tech Preview)
Use generated entry-point C++ classes in a main function. (Since R2026a)
Use MATLAB Class in MATLAB Function
- Generate Code for MATLAB Functions That Use Value Classes
Use a value class in MATLAB code intended for code generation. - Generate Code for MATLAB Functions That Use Handle Classes
Use a handle class in MATLAB code intended for code generation. - Generate Code for MATLAB Functions That Use System Objects
Use a System object in MATLAB code for code generation. - Generate Code for MATLAB Functions That Use Singleton Classes
Use a singleton handle class in MATLAB code for code generation.
Pass MATLAB Class as Entry-Point Input Argument
- Specify Value Class Objects as Inputs
Specify that an entry-point input is an object of a value class.
Troubleshooting
Resolve Error: Handle Object Referenced by Persistent Variable
Troubleshoot error when using a persistent handle object.
Resolve Error: Nontunable Property Cannot Be Defined by Subscripted Assignment
Troubleshoot code generation failure when a System object uses a structure.
Resolve Error: Class Properties Must Be Fully Defined Before Use
Troubleshoot code generation error when class properties are not defined before use.
Resolve Error: Cannot Reference Handle Object Outside of Loop
Troubleshoot error when creating or allocating a handle object inside a
for-loop.
Resolve Error: Class Does Not Have Property
Troubleshoot issues when you assign a value to a property of a handle class that is returned by a method.
Resolve Error: Passing by Reference Not Supported for Some Properties
Troubleshoot error when you use certain types of properties with
coder.ref, coder.rref, or
coder.wref.
Resolve Error: Code Generator Failed to Produce C++ Destructor for MATLAB Class
Troubleshoot generation of standalone code for C++ destructor of a MATLAB class.
Resolve Error: Code Generation Does Not Support String and Object Arrays
Troubleshoot error when you use an object or string array in your MATLAB code.
Resolve Error: coder.varsize Not Supported for Class Properties
Troubleshoot code generation error when using coder.varsize
with properties of a MATLAB class.
