Documentation

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

  1. Define the value class. For example, define a classmyRectangle.

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

  2. Define a function that takes an object of the value class as an input. For example:

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

  3. Create an object of the class.

    rect_obj = myRectangle(4,5)

    rect_obj = myRectangle with properties: length: 4 width: 5
  4. Pass the example object tocodegenby using the-argsoption.

    codegengetarea-args{rect_obj}-report

    In the code generation report, you see thatrhas the same properties,lengthandwidth, 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-argsoption.

  1. Create an object of the class:

    rect_obj = myRectangle(4,5)
    rect_obj = myRectangle with properties: length: 4 width: 5

  2. To create a type for an object ofmyRectanglethat has the same property types asrect_obj, usecoder.typeof.

    coder.typeofcreates acoder.ClassType对象为一个类定义了一个类型。

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

  3. Pass the type tocodegenby using the-argsoption.

    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 oft16-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.ClassTypeObject and Class Definition File

When you generate code, the properties of thecoder.ClassTypeobject that you pass tocodegenmust be consistent with the properties in the class definition file. 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.

限制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 withcoder.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).

See Also

Related Topics