MATLAB Classes
R2026bYou can generate code for MATLAB 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?.
You can:
Use a MATLAB value or handle class in MATLAB code.
Use a MATLAB value class as an input argument to an entry-point function or class.
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 generating C and C++ code for MATLAB classes.
Classes
coder.ClassType | Represent set of MATLAB classes acceptable for input specification |
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
Understand how the code generator maps the classes in your MATLAB code to C++ classes. - Generate Class Interface in C++ Code
Generate C++ code that is packaged into a class.
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.
Use MATLAB Class as Entry Point (Tech Preview)
- Code Generation for Entry-Point Classes
Understand usage notes and limitations for entry-point classes (Tech Preview). (Since R2026a) - Generate Standalone C++ Code for Entry-Point Class at the Command Line
Use a MATLAB class as an entry point for code generation (Tech Preview). (Since R2026a) - Generate Standalone C++ Code for Entry-Point Class Using MATLAB Coder App
Use a MATLAB class as an entry point in the MATLAB Coder™ app (Tech Preview). (Since R2026b) - Generate C++ Executable for Entry-Point System Object in a Namespace
Use a System object in a namespace as an entry point for code generation (Tech Preview). (Since R2026a) - Unit Test Generated Standalone Class
Run MATLAB unit tests on generated standalone classes (Tech Preview). (Since R2026b) - Generate Standalone C++ Classes to Represent a Physical System
Use generated entry-point C++ classes in a main function and produce an executable (Tech Preview). (Since R2026a)
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.
