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
creates at
= coder.typeof(value_class_object
)coder.ClassType
object for the objectvalue_class_object
.
creates at
= coder.newtype(value_class_name
)coder.ClassType
object for an object of the classvalue_class_name
.
Input Arguments
|
Value class object from which to create the v = myValueClass; t = coder.typeof(v); t = coder.typeof(myValueClass(2,3)); |
|
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.ClassType
objectt
from a value class objectv
by 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 ofmyRectangle
as 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.ClassType
object based onv
.
t = coder.typeof(v)
t = coder.ClassType 1×1 myRectangle length: 1×1 double width : 1×1 double
coder.typeof
creates acoder.ClassType
object that has the same properties names and types asv
has.
Generate code forgetarea
. Specify the input type by passing thecoder.ClassType
目的,t
, to the-args
选项。
codegengetarea-args{t}-report
通过使用编码器
创建一个coder.ClassType
object for an object of the value classmySquare
by using编码器
.
Create value classmySquare
that has one property,side
.
classdefmySquare特性side;endmethodsfunctionobj= mySquare(val)ifnargin > 0 obj.side = val;endendfunctiona = calcarea(obj) a = obj.side * obj.side;endendend
创建一个coder.ClassType
type formySquare
.
t = coder.newtype('mySquare')
Specify the type ofside
.
t.Properties.side = coder.typeof(2)
Tips
After you create a
coder.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 a
coder.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 the
coder.ClassType
object that you pass tocodegen
must 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.ClassType
object does not have to include those properties. The code generator removes properties that you do not use.
See Also
codegen
|coder
|coder.ArrayType
|coder.Constant
|coder.EnumType
|coder.EnumType
|coder.FiType
|Coder.primitiveType
|coder.Type
|Coder.cstructname
|编码器
|coder.resize
|coder.typeof