Main Content

MATLAB Classes

Code generation for 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.ClassTypeRepresent set of MATLAB classes acceptable for input specification

Objects

coder.ClassSignatureEntry-point class for code generation (Tech Preview) (Since R2026a)
coder.FunctionSignatureEntry-point function or method for code generation (Tech Preview) (Since R2026a)

Functions

addMethodAdd method specification to coder.ClassSignature object (Tech Preview) (Since R2026a)

Topics

Code Generation Basics

Use MATLAB Class as Entry Point (Tech Preview)

Use MATLAB Class in MATLAB Function

Pass MATLAB Class as Entry-Point Input Argument

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.

Featured Examples