Main Content

coder.FunctionSignature

R2026b

Entry-point function or method 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.FunctionSignature object to specify an entry-point function or a method of an entry-point class.

To generate code for coder.FunctionSignature object:

  • If the function signature object specifies an entry-point function, pass the function signature object to the codegen command by using the -function option.

  • If the function signature object specifies a method of an entry-point class, pass the class signature object that represents the entry-point class to the codegen command by using the -class option.

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.

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

example

Input Arguments

expand all

Name of the 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 function or method, 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.

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 the 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.

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

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

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

  • If the polymorphic method is not the class constructor and you specify the InterfaceName property, 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 property, the code generator produces overloaded C++ methods.

Examples

collapse all

Create a function signature object for a function that uses an arguments block to specify the input argument.

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 function signature object that represents foo. You do not need to specify input arguments because the code generator infers these arguments from the arguments block in the function body. The function signature object displays the function arguments as "Auto".

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

  FunctionSignature with properties:

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

Create a function signature object for a function and explicitly specify the input arguments.

Examine this MATLAB function, which returns the product of the two input arguments.

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

At the command line, create a function signature object that represents foo. Pass the input arguments as a cell array. For this example, specify the InterfaceName argument to instruct the code generator to use the name myFooFunction in the generated code.

fsObj = coder.FunctionSignature("foo",{0,0},InterfaceName="myFooFunction")
fsObj = 

  FunctionSignature with properties:

             Name: "foo"
             Args: {[1×1 coder.PrimitiveType]  [1×1 coder.PrimitiveType]}
          Nargout: []
    InterfaceName: "myFooFunction"

Specify the methods of an entry-point class by creating coder.FunctionSignature objects.

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}

Version History

Introduced in R2026a