coder.ClassSignature
Description
Use a coder.ClassSignature object to specify an entry-point class
for code generation. To generate code for a coder.ClassSignature object, pass
the object to the codegen command by using the
-class option.
You can also use a coder.ClassSignature when you generate code for an
entry-point function that takes a MATLAB® class as an input argument. Pass the coder.ClassSignature
object to the codegen command by using the -args
option. You must also specify this object as an entry point by using the
-class argument.
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
Syntax
Description
creates a classSig = coder.ClassSignature(classInstance)coder.ClassSignature object from an instance of a MATLAB class, classInstance.
creates a classSig = coder.ClassSignature(classType)coder.ClassSignature object from the coder.ClassType object classType.
Input Arguments
Name of a MATLAB class, specified as a string scalar.
Example: classSig =
coder.ClassSignature("Rectangle")
Instance of a MATLAB class, specified as MATLAB class object or an expression that evaluates to a MATLAB class object.
Example: classSig =
coder.ClassSignature(Rectangle(2,4))
coder.Type object representing a MATLAB class, specified as coder.ClassType object.
Example: classSig =
coder.ClassSignature(coder.typeof(Rectangle(3,4)))
Output Arguments
Class specification, returned as a coder.ClassSignature
object.
Properties
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
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. You can modify the
property specifications by modifying the fields of the Properties
structure. For example, to specify that the data type of property
Height of class Rectangle is an unbounded row
vector or 8-bit unsigned integer, use this
command:
classSig.Properties.Height = coder.typeof(int8(0),[1 Inf],[false true])
Code generation supports fixed-size and unbounded variable-size class properties. The code generator accepts bounded variable-size class properties, but it may not preserve the upper bound in the generated code.
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 containing a vector of coder.FunctionSignature objects.
You can modify 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
addMethod | Add method specification to coder.ClassSignature object (Tech
Preview) |
Examples
Create a coder.ClassSignature object from the
Square class, and add method specifications to it by using the
addMethod function.
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
classSig 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 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
Create a coder.ClassSignature object from an
instance of the Square class. Add method specifications to the object
by using the addMethod function.
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,
and the getArea method returns the area of a
Square object.
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 by using an instance of the class. When you create a
coder.ClassSignature object by using an instance of the class,
MATLAB uses the properties of the class instance to define the properties of the
coder.ClassSignature object.
classSig = coder.ClassSignature(Square(0))
classSig =
coder.ClassSignature
1×1 Square
TypeName: "Square"
Properties:
side: 1×1 single
Methods: dictionary (string --> cell) with no entries.Add the methods of the Square class to
classSig by using the addMethod function. The
arguments that you specify for the constructor function must be compatible with the
property definitions in the classSig object. View the
coder.ClassSignature object. MATLAB represents 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:
side: 1×1 single
Methods:
Square:
Args: {1×1 single}
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
Create a coder.ClassSignature object from a
coder.ClassType object that represents the Rectangle class.
Add methods to the object directly.
Examine the value class Rectangle, which has two properties,
Length and Width. The class has three methods.
The constructor method, Rectangle, creates an instance of the
Rectangle class. The getArea method returns the
area of a Rectangle object and the method
isEqualArea tests whether 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
Use coder.typeof to create a
coder.Type object that represents the Rectangle
class.
recType = coder.typeof(Rectangle(0,0))
recType =
coder.ClassType
1×1 Rectangle
Properties:
Length: 1×1 double
Width: 1×1 doubleCreate a coder.ClassSignature object from the
recType object that represents the Rectangle
class. When you create a coder.ClassSignature object by using a
coder.Type object, MATLAB uses the properties of the type object to define the properties of the
coder.ClassSignature object.
classSig = coder.ClassSignature(recType)
classSig =
coder.ClassSignature
1×1 Rectangle
TypeName: "Rectangle"
Properties:
Length: 1×1 double
Width: 1×1 double
Methods: dictionary (string --> cell) with no entries.Add the methods of the Rectangle class to
classSig by directly modifying the Methods
dictionary of the object. The arguments that you specify for the constructor function
must be compatible with the property definitions in the classSig
object. View the coder.ClassSignature object. MATLAB represents method arguments that are instances of the enclosing class by
using the keyword
this.
classSig.Methods{"Rectangle"}.Args = {0,0};
classSig.Methods{"getArea"}.Args = {classSig};
classSig.Methods{"isEqualArea"}.Args = {classSig,classSig}classSig =
coder.ClassSignature
1×1 Rectangle
TypeName: "Rectangle"
Properties:
Length: 1×1 double
Width: 1×1 double
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
You can modify or delete the methods of a
coder.ClassSignature object by using MATLAB dictionary syntax.
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 = 3
end
methods
function obj = Square(val)
if nargin == 1
obj.side = double(val);
end
end
function out = getArea(obj)
out = obj.side*obj.side;
end
function out = scale(obj,factor)
out = getArea(obj)*factor;
end
end
endCreate a coder.ClassSignature object that represents the
Square class by using the class name. Add the methods of the
Square class to classSig by using the
addMethod
function.
classSig = coder.ClassSignature("Square"); 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}In this example, the constructor method and the scale method are
polymorphic. Specify that these methods can also accept single
values.
addMethod(classSig,"Square",{single(0)}); addMethod(classSig,"scale",{classSig,single(0)})
ans =
coder.ClassSignature
1×1 Square
TypeName: "Square"
Properties: struct with no fields.
Methods:
Square:
(1) Args: {1×1 double}
(2) Args: {1×1 single}
getArea:
Args: {1×1 this}
scale:
(1) Args: {1×1 this, 1×1 double}
(2) Args: {1×1 this, 1×1 single}Rename the scale methods in the generated code. Specify a new
name for each method by using the InterfaceName
property.
classSig.Methods{"scale"}(1).InterfaceName = "doubleScale";
classSig.Methods{"scale"}(2).InterfaceName = "singleScale"
classSig =
coder.ClassSignature
1×1 Square
TypeName: "Square"
Properties: struct with no fields.
Methods:
Square:
(1) Args: {1×1 double}
(2) Args: {1×1 single}
getArea:
Args: {1×1 this}
scale:
(1) Args: {1×1 this, 1×1 double}
InterfaceName: "doubleScale"
(2) Args: {1×1 this, 1×1 single}
InterfaceName: "singleScale"Modify the second Square method signature and specify that this
method can accept zero arguments. Add a signature to the scale method
that accepts a variable-length row vector of doubles. Set the
InterfaceName property of this signature to
"multiScale". Delete the "singleScale"
signature.
classSig.Methods{"Square"}(2).Args = {};
addMethod(classSig,"scale",{classSig,coder.typeof(0,[1 10],[false true])},InterfaceName="multiScale");
classSig.Methods{"scale"}(2) = []classSig =
coder.ClassSignature
1×1 Square
TypeName: "Square"
Properties: struct with no fields.
Methods:
Square:
(1) Args: {1×1 double}
(2) Args: {}
getArea:
Args: {1×1 this}
scale:
(1) Args: {1×1 this, 1×1 double}
InterfaceName: "doubleScale"
(2) Args: {1×1 this, 1×:10 double}
InterfaceName: "multiScale"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
Examine the declaration of the Square class in the
square.h
file.
class Square {
public:
void init(double val);
void init();
double getArea() const;
double doubleScale(double factor) const;
void multiScale(const double factor_data[], const int factor_size[2],
double out_data[], int out_size[2]) const;
explicit Square(double val);
explicit Square(user_construct_tag_t tag);
Square();
~Square();
double side;
};The code generator uses the InterfaceName values to name the
doubleScale and multiScale methods. The code
generator produces three constructor methods:
explicit Square(double val), which maps to the constructor that accepts a double value.explicit Square(user_construct_tag_t tag), which maps to the constructor that accepts zero arguments.Square(), which is the non- initializing constructor.
The external C++ application can interact directly with the public
Square constructors and with the getArea,
doubleScale, and multiScale methods. The
init method and the non-initializing constructor are internal
methods. Because these methods may change in the future, do not use them in your custom
C++ code. To learn how to use the user_construct_tag_t tag in your
C++ code, see Generate C++ Executable for Entry-Point System Object in a Namespace (Tech Preview).
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2026a
See Also
addMethod | codegen | coder.FunctionSignature | coder.typeof
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)