Contenuto principale

coder.ClassSignature

R2026b

Entry-point class specification (Tech Preview)

Since R2026a

Description

Note

Using a MATLAB® class as an entry point for code generation is a tech preview. This feature is in active development and might change between the tech preview and the general release. To enable the feature, enter enableCodegenForEntryPointClasses at the command line before launching the MATLAB Coder™ app, calling the codegen function, or creating a coder.Type object. To provide feedback, email the development team or participate in a survey.

Use a coder.ClassSignature object to specify an entry-point class for code generation.

To generate code for a class signature object, pass the object to the codegen command by using the -class option.

You can also use a coder.ClassSignature object as an input argument to an entry-point function. Specify the class signature object as an input argument by using the -args option of the codegen command. When you use a class signature object as an input to an entry-point function, you must simultaneously specify the class signature object as an entry-point class by using the -class option.

The code generator only generates a stable interface for a method of an entry-point class when you add the method specification to the coder.ClassSignature object. The code generator can change or inline other methods of the entry-point class.

Creation

Description

classSig = coder.ClassSignature(className) creates a coder.ClassSignature object from the MATLAB class named className.

example

classSig = coder.ClassSignature(classInstance) creates a coder.ClassSignature object from an instance of a MATLAB class, classInstance.

example

classSig = coder.ClassSignature(classType) creates a coder.ClassSignature object from the coder.ClassType object classType.

example

Input Arguments

expand all

Name of a MATLAB class, specified as a string scalar.

Example: classSig = coder.ClassSignature("Rectangle")

Instance of a MATLAB class, specified as a MATLAB class object or an expression that evaluates to a MATLAB class object.

Example: classSig = coder.ClassSignature(Rectangle(2,4))

Representation of a MATLAB class, specified as a coder.ClassType object.

Example: classSig = coder.ClassSignature(coder.typeof(Rectangle(3,4)))

Output Arguments

expand all

Class specification, returned as a coder.ClassSignature object.

Properties

expand all

Name of the MATLAB class represented by the coder.ClassSignature object, returned as a string scalar.

Name of the coder.ClassSignature object in the generated code, returned as a string scalar. Use the TypeName property to distinguish among multiple instances of the same MATLAB class. If you generate code for multiple instances of the same MATLAB class, the corresponding coder.ClassSignature objects must have different property specifications and different TypeName values.

Data types of the properties of the MATLAB class represented by the coder.ClassSignature object, returned as a structure. When you create a coder.ClassSignature from a MATLAB class name, the code generator does not populate this structure and instead infers class properties from the class constructor method.

You can add or alter the property specifications by modifying the fields of the Properties structure. For example, to specify that the data type of the property Height of the class Rectangle is an unbounded row vector of 8-bit unsigned integers, use this command:

classSig.Properties.Height = coder.typeof(int8(0),[1 Inf],[false true])

When you specify property types explicitly in the Properties structure, the specified types must be compatible with the property types defined in the class constructor method.

Code generation supports fixed-size and unbounded variable-size class properties. Code generation does not support bounded variable-size class properties for entry-point classes.

Specification of the methods of the MATLAB class represented by the coder.ClassSignature object, returned as a MATLAB string --> cell dictionary. In the Methods dictionary, each key is a method name, specified as a string scalar, and each value is a scalar cell array that contains a vector of coder.FunctionSignature objects.

You can alter the method specifications by modifying the Methods dictionary. For example, to set the InterfaceName property of the getArea method, use this command:

classSig.Methods{"getArea"}.InterfaceName = "myGetAreaMethod"

Object Functions

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

Examples

collapse all

Create a coder.ClassSignature object by specifying the name of the Square class. When you create a class signature object from the class name, the code generator infers the class properties from the class constructor method.

Examine the value class Square, which has one property, side. The class has three methods. The constructor method, Square, creates an instance of the Square class. The getArea method returns the area of a Square object, and the scale method returns the area of a Square object multiplied by factor.

classdef Square
    properties
        side
    end
    methods
        function obj = Square(val)
            obj.side = val;
        end
        function out = getArea(obj)
            out = obj.side*obj.side;
        end
        function out = scale(obj,factor)
            out = getArea(obj)*factor;
        end
    end
end

Create a coder.ClassSignature object that represents the Square class. Because the code generator infers the class properties from the constructor method, the class signature object does not include property definitions.

classSig = coder.ClassSignature("Square")
classSig = 

coder.ClassSignature
  1×1 Square
    TypeName: "Square"
    Properties: struct with no fields.
    Methods: dictionary (string --> cell) with no entries.

Create a coder.ClassSignature object by using an expression that evaluates to an instance of a MATLAB class. The code generator uses the properties of the class instance to define the properties of the class signature object. When you add the class constructor method to the class signature object, the arguments that you specify for the constructor function must be compatible with these property definitions.

Create a coder.ClassSignature object that represents the Square class by using an instance of the class. The class signature object displays properties derived from the class instance.

classSig = coder.ClassSignature(Square(0))
classSig = 

coder.ClassSignature
  1×1 Square
    TypeName: "Square"
    Properties:
      side: 1×1 double
    Methods: dictionary (string --> cell) with no entries.

Create a coder.ClassSignature object by using a coder.ClassType object. The code generator uses the properties of the type object to define the properties of the class signature object. When you add the class constructor method to the class signature object, the arguments that you specify for the constructor function must be compatible with these property definitions.

Create a coder.ClassType object that represents the Square class by using coder.typeof.

sqType = coder.typeof(Square(0))
sqType = 

coder.ClassType
  1×1 Square
    Properties:
      side: 1×1 double

Create a coder.ClassSignature object from the sqType object. The class signature object displays properties derived from the coder.ClassType object.

classSig = coder.ClassSignature(sqType)
classSig = 

coder.ClassSignature
  1×1 Square
    TypeName: "Square"
    Properties:
      side: 1×1 double
    Methods: dictionary (string --> cell) with no entries.

You can modify or delete the method specifications of a coder.ClassSignature object by using MATLAB dictionary syntax.

Examine the coder.ClassSignature object that represents the value class Square. This object specifies three methods.

classSig = 

coder.ClassSignature
  1×1 Square
    TypeName: "Square"
    Properties: struct with no fields.
    Methods:
      Square:
        Args: {1×1 double}
      getArea:
        Args: {1×1 this}
      scale:
        Args: {1×1 this, 1×1 double}

To change a method specification, modify the properties of the coder.FunctionSignature object in the Methods dictionary. For example, use the InterfaceName property to specify a different name for the getArea method in the generated code.

classSig.Methods{"getArea"}.InterfaceName = "SquareGetArea";

To delete a method specification, assign an empty array [] to the key for that method:

 classSig.Methods{"scale"} = [];

Alternatively, use the remove dictionary function:

classSig.Methods = remove(classSig.Methods,"scale");

Limitations

  • You cannot use coder.Constant to directly assign a constant value to a property of a class signature object. To define a property of an entry-point class as a constant, use the Constant attribute in the MATLAB class definition. Alternatively, specify a coder.Constant value when you add the constructor method to the class signature object.

  • If a property of an entry-point class is a handle class, that property must be private or protected.

  • You cannot use these types of classes as entry points:

    • Abstract classes

    • Classes contained in aggregate data types, such as structures, cells, and other classes

    • Classes that inherit from the matlab.mixin.Copyable class

Version History

Introduced in R2026a