주요 콘텐츠

Class Limitations for Code Generation

You can generate code for MATLAB® functions that use value classes and handle classes. By default, MATLAB classes are value classes. To create a handle class, you must derive the class from the handle superclass. For more information about value and handle classes in MATLAB, see Comparison of Handle and Value Classes.

You can also use a System object™ in MATLAB code for code generation. A System object is a type of handle class that derives from the matlab.System class. To learn about generating code for System objects, see in System Objects in MATLAB Code Generation.

When you generate code for a MATLAB function that uses a class, certain considerations and limitations apply. 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.

Limitations for Value and Handle Classes

When you use value and handle classes in MATLAB code for code generation, follow these restrictions:

  • You can generate code for entry-point MATLAB functions that use classes, but you cannot generate code directly for a MATLAB class.

    For example, if MyClass is a class definition, you cannot generate code by executing:

    codegen MyClass

  • 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 use coder.ceval with 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, or subsindex.

  • If a subclass derives from a MATLAB built-in type, the subclass must be an enumeration class. See Code Generation for Enumerations.

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 AbortSet property attribute is not supported.

See Also

| | |

Topics