主要内容

addMethod

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

Since R2026a

    Description

    When you specify a MATLAB® class as an entry point for code generation, use the addMethod function to add methods to the coder.ClassSignature object.

    Note

    Using a MATLAB class as an entry point for code generation is a tech preview. This feature is in active development and may change between the tech preview and the general release. The primary purpose of the tech preview is to solicit feedback from users. To enable the feature, enter enableCodegenForEntryPointClasses at the command line before calling the codegen function or creating a coder.Type object in a MATLAB session. To provide feedback, email the development team or participate in a survey.

    addMethod(classSig,fsObj) adds the method specified by the coder.FunctionSignature object fsObj to the coder.ClassSignature object classSig.

    example

    addMethod(classSig,methodName,args) adds the method methodName with the arguments args to the coder.ClassSignature object classSig.

    example

    addMethod(classSig,methodName,args,Name=Value) specifies options for methodName by using one or more name-value arguments. For example, to set the number of output arguments to two, set Nargout to 2.

    Examples

    collapse all

    Specify the methods of an entry-point class by specifying the methods as coder.FunctionSignature objects and then adding these specifications to a coder.ClassSignature object.

    Examine the value class Rectangle. The Rectangle class constructor defines a Rectangle object that has a length and width specified by the properties Length and Width. The getArea method calculates the area of a Rectangle object. The isEqualArea method uses the getArea method to determine if two Rectangle objects have the same area.

    classdef Rectangle
        properties
            Length 
            Width
        end
        methods
            function obj = Rectangle(length,width)
                obj.Length = length;
                obj.Width = width;
            end
            function area = getArea(obj)
                area = obj.Length*obj.Width;
            end
            function out = isEqualArea(obj1,obj2)
                out = isequal(getArea(obj1),getArea(obj2));
            end        
        end
    end

    Create a coder.ClassSignature object that represents the Rectangle class. When you create a coder.ClassSignature object by using the class name, the code generator infers the class properties from the constructor method.

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

    Create coder.FunctionSignature objects that represent methods of the Rectangle class. Specify an input argument that is an instance of the enclosing class by using the coder.ClassSignature object.

    recFs = coder.FunctionSignature("Rectangle",{0,0});
    gaFs = coder.FunctionSignature("getArea",{classSig});
    ieaFs = coder.FunctionSignature("isEqualArea",{classSig,classSig});

    Use the addMethod function to add the coder.FunctionSignature objects to the coder.ClassSignature object. View the coder.ClassSignature object. MATLAB represents method arguments that are instances of the enclosing class by using the keyword this.

    addMethod(classSig,recFs);
    addMethod(classSig,gaFs);
    addMethod(classSig,ieaFs)
    ans = 
    
    coder.ClassSignature
      1×1 Rectangle
          TypeName: "Rectangle"
        Properties: struct with no fields.
           Methods:
             Rectangle:
               Args: {1×1 double, 1×1 double}
             getArea:
               Args: {1×1 this}
             isEqualArea:
               Args: {1×1 this, 1×1 this}

    Generate code for the Rectangle class by passing the coder.ClassSignature object to the codegen command with the -class option.

    codegen -config:lib -lang:c++ -class classSig

    Specify the methods of an entry-point class directly by adding the method information to a coder.ClassSignature object.

    Examine the value class Square, which has one property, side. The class has two methods. The constructor method, Square, creates an instance of the Square class. The method getArea returns the area of an instance of the Square class.

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

    Create a coder.ClassSignature object that represents the Square class. When you create a coder.ClassSignature object by using the class name, the code generator infers the class properties from the constructor method.

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

    Add the methods of the Square class to the classSig object by using the addMethod function. Specify an input argument that is an instance of the enclosing class by using the coder.ClassSignature object. View the coder.ClassSignature object. MATLAB represents the method arguments that are instances of the enclosing class by using the keyword this.

    addMethod(classSig,"Square",{0});
    addMethod(classSig,"getArea",{classSig})
    ans = 
    
    coder.ClassSignature
      1×1 Square
          TypeName: "Square"
        Properties: struct with no fields.
           Methods:
             Square:
               Args: {1×1 double}
             getArea:
               Args: {1×1 this}

    Generate code for the Square class by passing the coder.ClassSignature object to the codegen command with the -class option.

    codegen -config:lib -lang:c++ -class classSig

    Input Arguments

    collapse all

    Class specification for code generation, specified as a coder.ClassSignature object.

    Example: addMethod(classSig,"scale",{classSig,0})

    Name of the method to add to the coder.ClassSignature object, specified as a string scalar or character vector.

    Example: addMethod(classSig,"getArea",{classSig})

    Input arguments of the method methodName, specified as a cell array of example values, coder.Type objects, or coder.ClassSignature objects. The position of each element in the cell array must correspond to the position of the input argument in the function definition. To create a coder.Type object, use coder.typeof or coder.newtype.

    Example: addMethod(classSig,"scale",{classSig,coder.typeof(int8(0))})

    Method specification to add to the classSig object, specified as a coder.FunctionSignature object.

    Example: addMethod(classSig,myFuncSig)

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: InterfaceName="myArea",Nargout=4

    Name for the method methodName in the generated code, specified as a string scalar or character vector.

    Example: addMethod(classSig,"getArea",{classSig},InterfaceName="calcArea")

    Number of output arguments to generate for the method MethodName in the generated code, specified as an integer.

    Example: addMethod(classSig,"myOps",{classSig,0,0},Nargout=2)

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2026a