Hauptinhalt

addMethod

R2026b

Add method specification to coder.ClassSignature object (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.

    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. At a minimum, you must add the class constructor method. If the MATLAB class does not define a constructor, add the default constructor.

    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.

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

    example

    Examples

    collapse all

    Add method specification to a coder.ClassSignature object.

    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. When you create a class signature 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. The class signature object represents the method arguments that are instances of the enclosing class by using the keyword this.

    addMethod(classSig,"Square",{0});
    addMethod(classSig,"getArea",{classSig});
    addMethod(classSig,"scale",{classSig,0})
    ans = 
    
    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}

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

    Examine the value class Square.

    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.

    classSig = coder.ClassSignature("Square");

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

    sqFS = coder.FunctionSignature("Square",{0,0});
    gaFS = coder.FunctionSignature("getArea",{classSig});
    scFS = coder.FunctionSignature("scale",{classSig,0});

    Use the addMethod function to add the coder.FunctionSignature objects to the coder.ClassSignature object.

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

    Input Arguments

    collapse all

    Representation of entry-point class, 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. You can add only public methods. The code generator infers private and protected method definitions from the MATLAB code.

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

    Input arguments of the method methodName, specified as a cell array that can contain example values, coder.Type objects, and 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. The first argument must be the coder.ClassSignature object to which you add the method. 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 of the method in the generated code, specified as a string scalar or character vector.

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

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

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

    Limitations

    • You cannot add these types of methods to a coder.ClassSignature object:

    Tips

    • The class constructor method must define all properties of the class, including all structure fields, cell array elements, and properties of contained classes.

    • If the entry-point class is a System object™, you must add the System object step method to the class signature object. Because step supports a variable number of outputs, you must specify the number of output arguments when you add the method. For an example, see Generate C++ Executable for Entry-Point System Object in a Namespace.

    Version History

    Introduced in R2026a