主要内容

coder.FunctionSignature

Entry-point function or method for code generation (Tech Preview)

Since R2026a

Description

Use a coder.FunctionSignature object to specify an entry-point function or method. To generate code for a coder.FunctionSignature object, pass the object to the codegen command by using the -function option. To generate code for a method that is a coder.FunctionSignature object, you must specify the enclosing class as an entry-point class.

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.

Creation

To create a coder.FunctionSignature object, use the coder.FunctionSignature function. Alternatively, the code generator creates this object when you:

  • Add a method to a coder.ClassSignature object by using the addMethod method.

  • Add a method to a coder.ClassSignature object by modifying the dictionary in the Methods property.

Description

fs = coder.FunctionSignature(name) creates a function signature object for the function name. Use this syntax to instruct the code generator to infer input arguments from an arguments block or assert statements in the function body. You cannot use this syntax if name is a method.

example

fs = coder.FunctionSignature(name,args) creates a function signature object for the function or method name with the input arguments args. If name is a method, you must specify the enclosing class as an entry point when you generate code.

example

fs = coder.FunctionSignature(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments in the previous syntax. For example, to set the number of output arguments to two, set Nargout to 2.

Input Arguments

expand all

Name of function or method, specified as a string or character vector. If name is a method, do not include the class prefix.

This argument sets the Name property as a string scalar.

Example: fsObj = coder.FunctionSignature("myFunction")

Input arguments to the method or function name, specified as a cell array of example values and coder.ClassType objects or as "Auto". If you specify a cell array, the position of each element in the cell array must correspond to the position of the input argument in the function or method definition. To indicate that the function or method takes no input arguments, use an empty cell array, {}.

If name is a function, use "Auto" to instruct the code generator to infer the input argument definitions from the arguments block or from assert statements in the function body. If name is a method, "Auto" is not supported.

This argument sets the Args property.

Example: fsObj = coder.FunctionSignature("myMethod",{classSig,ones(4,4),coder.typeof("hello")})

Example: fsObj = coder.FunctionSignature("myFunction",{})

Name-Value Arguments

expand 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 function or method interface in the generated code, specified as a string scalar or character vector. If you do not specify an interface name, the code generator derives the name of the generated function or method from the name of the MATLAB function or method.

When you generate C code for a polymorphic method, you must specify the InterfaceName argument.

When you generate C++ code for a polymorphic method:

  • If the polymorphic method is the class constructor, the code generator ignores the InterfaceName argument. The code generator produces overloaded C++ constructor methods.

  • If the polymorphic method is not the class constructor and you specify the InterfaceName argument, the code generator uses the InterfaceName to name the generated method.

  • If the polymorphic method is not the class constructor and you do not specify the InterfaceName argument, the code generator produces overloaded C++ methods.

This argument sets the InterfaceName property.

Example: fsObj = coder.FunctionSignature("myFunction",{0,0},InterfaceName="MyRenamedFunction")

Number of output arguments to generate for function or method name, specified as an integer. If you do not specify a value, the code generator infers the number of output arguments from the MATLAB code.

This argument sets the Nargout property.

Example: fsObj = coder.FunctionSignature("myFunction",Nargout=2)

Example: fsObj = coder.FunctionSignature("myMethod",{classSig,ones(4,4),coder.typeof("hello")},Nargout=2)

Output Arguments

expand all

Function or method definition, returned as a coder.FunctionSignature object.

Properties

expand all

Name of function or method, specified as a string scalar or character vector.

Input arguments to the function or method Name, specified as a cell array of example values and coder.Type objects or "Auto". If you specify args as a cell array of example values and coder.Type objects, the property stores the values as a cell array of coder.Type objects. If you specify args as "Auto", the code generator infers the input arguments from the body of the function.

Number of output arguments to generate for function or method Name, specified as an integer. If you do not specify the Nargout argument, the code generator infers the number of output arguments from the MATLAB code.

Name of the function or method interface in the generated code, specified as a string scalar or character vector. If you do not specify an interface name, the code generator derives the name from the name of the MATLAB function or method.

Examples

collapse all

Generate code for a MATLAB function by using coder.FunctionSignature to specify the function as an entry point for code generation.

Examine this MATLAB function, which returns the product of the two input arguments. This function uses an arguments block to specify that the input arguments are scalar doubles.

function out = foo(a,b) %#codegen
arguments
    a (1,1) double
    b (1,1) double
end
out = a*b;
end

At the command line, create a FunctionSignature object for foo. You do not need to specify input arguments because the code generator can infer these arguments from the arguments block in the function body.

fsObj = coder.FunctionSignature("foo")
fsObj = 

  FunctionSignature with properties:

             Name: "foo"
             Args: "Auto"
          Nargout: []
    InterfaceName: ''

Generate a MEX function for foo. Specify fsObj as an entry-point function by using the -function option of the codegen command.

codegen -function fsObj
Code generation successful.

Test the MEX function. The MEX function produces the expected output.

foo_mex(3,4)
ans =

    12

Generate code for an entry-point MATLAB class and use coder.FunctionSignature objects to specify the methods of the class.

Examine the value class Rectangle. The Rectangle class constructor defines a Rectangle object with 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. Then, 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

Extended Capabilities

expand all

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

Version History

Introduced in R2026a