Documentation

coder.ClassType class

Package:coder
Superclasses:coder.ArrayType

Represent set ofMATLABclasses

Description

Specifies the set of value class objects that the generated code can accept. Use only with thecodegen-args选项。Do not pass as an input to a generated MEX function.

Construction

t= coder.typeof(value_class_object)creates acoder.ClassTypeobject for the objectvalue_class_object.

t= coder.newtype(value_class_name)creates acoder.ClassTypeobject for an object of the classvalue_class_name.

Input Arguments

value_class_object

Value class object from which to create thecoder.ClassType目的。value_class_objectis an expression that evaluates to an object of a value class. For example:

v = myValueClass; t = coder.typeof(v);

t = coder.typeof(myValueClass(2,3));

value_class_name

Name of a value class definition file on the MATLAB®path. Specify as a character vector or string scalar. For example:

t = coder.newtype('myValueClass');

Properties

When you create acoder.ClassTypeobjecttfrom a value class objectvby usingcoder.typeof, the properties oft与属性相同v与属性持续的set tofalse.

Copy Semantics

Value. To learn how value classes affect copy operations, seeCopying Objects(MATLAB).

Examples

Create Type Based on Example Object

创建一个type based on an example object in the workspace.

创建一个value classmyRectangle.

classdefmyRectangle特性length; width;endmethodsfunctionobj= myRectangle(l,w)ifnargin > 0 obj.length = l; obj.width = w;endendfunctionarea = calcarea(obj) area = obj.length * obj.width;endendend

创建一个function that takes an object ofmyRectangleas an input.

functionz = getarea(r)%#codegenz = calcarea(r);end

创建一个n object ofmyRectangle.

v = myRectangle(1,2)
v = myRectangle with properties: length: 1 width: 2

创建一个coder.ClassTypeobject based onv.

t = coder.typeof(v)
t = coder.ClassType 1×1 myRectangle length: 1×1 double width : 1×1 double

coder.typeofcreates acoder.ClassTypeobject that has the same properties names and types asvhas.

Generate code forgetarea. Specify the input type by passing thecoder.ClassType目的,t, to the-args选项。

codegengetarea-args{t}-report

通过使用编码器

创建一个coder.ClassTypeobject for an object of the value classmySquareby using编码器.

Create value classmySquarethat has one property,side.

classdefmySquare特性side;endmethodsfunctionobj= mySquare(val)ifnargin > 0 obj.side = val;endendfunctiona = calcarea(obj) a = obj.side * obj.side;endendend

创建一个coder.ClassTypetype formySquare.

t = coder.newtype('mySquare')

Specify the type ofside.

t.Properties.side = coder.typeof(2)

Tips

  • After you create acoder.ClassType, you can modify the types of the properties. For example:

    t = coder.typeof(myClass) t.Properties.prop1 = coder.typeof(int16(2)); t.Properties.prop2 = coder.typeof([1 2 3]);

  • After you create acoder.ClassType,您可以添加属性。例如:

    t = coder.typeof(myclass)t.properties.newprop1 = coder.typeof(int8(2));t.properties.newprop2 = coder.typeof([1 2 3]);

  • When you generate code, the properties of thecoder.ClassTypeobject that you pass tocodegenmust be consistent with the properties in the class definition file. However, if the class definition file has properties that your code does not use, thecoder.ClassTypeobject does not have to include those properties. The code generator removes properties that you do not use.

Introduced in R2017a