Specify Objects as Inputs at the Command Line
If you generate code by usingcodegen
, to specify the type of an input that is a value class object, you can provide an example object with the-args
option.
Define the value class. For example, define a class
myRectangle
.classdefmyRectanglepropertieslength; width;endmethodsfunctionobj = myRectangle(l,w)ifnargin > 0 obj.length = l; obj.width = w;endendfunctionarea = calcarea(obj) area = obj.length * obj.width;endendend
Define a function that takes an object of the value class as an input. For example:
functionz = getarea(r)%#codegenz = calcarea(r);end
Create an object of the class.
rect_obj = myRectangle(4,5)
rect_obj = myRectangle with properties: length: 4 width: 5
Pass the example object to
codegen
by using the-args
option.codegengetarea-args{rect_obj}-report
In the code generation report, you see that
r
has the same properties,length
andwidth
, as the example objectrect_object
. The properties have the same size and type as they do in the example object,rect_object
.
Instead of providing an example object, you can create a type for an object of the value class, and then provide the type with the-args
option.
Create an object of the class:
rect_obj = myRectangle(4,5)
rect_obj = myRectangle with properties: length: 4 width: 5
To create a type for an object of
myRectangle
that has the same property types asrect_obj
, usecoder.typeof
.coder.typeof
creates acoder.ClassType
对象为一个类定义了一个类型。t= coder.typeof(rect_obj)
t = coder.ClassType 1×1 myRectangle length: 1×1 double width : 1×1 double
Pass the type to
codegen
by using the-args
option.codegengetarea-args{t}-report
After you create a type for a value class, you can change the types of the properties. For example, to make the properties oft
16-bit integers:
t.Properties.length = coder.typeof(int16(1)) t.Properties.width = coder.typeof(int16(1))
You can also add or delete properties. For example, to add a propertynewprop
:
t.Properties.newprop = coder.typeof(int16(1))
Consistency Betweencoder.ClassType
Object and Class Definition File
When you generate code, the properties of thecoder.ClassType
object that you pass tocodegen
must be consistent with the properties in the class definition file. 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.
限制s for Using Objects as Entry-Point Function Inputs
Entry-point function inputs that are objects have these limitations:
An object that is an entry-point function input must be an object of a value class. Objects of handle classes cannot be entry-point function inputs. Therefore, a value class that contains a handle class cannot be an entry-point function input.
An object cannot be a global variable.
If an object has duplicate property names, you cannot use it with
coder.Constant
. Duplicate property names occur in an object of a subclass in these situations:The subclass has a property with the same name as a property of the superclass.
The subclass derives from multiple superclasses that use the same name for a property.
For information about when MATLAB®allows duplicate property names, seeSubclassing Multiple Classes(MATLAB).