Specify Objects as Inputs in the MATLAB Coder App - MATLAB & Simulink (original) (raw)

Main Content

In the MATLAB® Coder™ app, to specify the type of an input that is a value class object:

  1. Define the value class. For example, define a classmyRectangle.
    classdef myRectangle
    properties
    length;
    width;
    end
    methods
    function obj = myRectangle(l,w)
    if nargin > 0
    obj.length = l;
    obj.width = w;
    end
    end
    function area = calcarea(obj)
    area = obj.length * obj.width;
    end
    end
    end
  2. Define a function that takes an object of the value class as an input. For example:
    function z = getarea(r)
    %#codegen
    z = calcarea(r);
    end
  3. In the app, create a project for getarea. On theDefine Input Types page, specify the type of the object in one of these ways:

Automatically Define an Object Input Type

Provide an Example

If you provide an object of the value class, the app uses the sizes and types of the properties of the example object.

  1. In MATLAB, define an object of the value classmyRectangle.
    rect_obj = myRectangle(4,5)
  2. In the app, on the Define Input Types page, clickLet me enter input or global types directly.
  3. Click the field to the right of the input parameterr.
  4. Select .
  5. Enter rect_obj or select it from the list of workspace variables.
    The app determines the properties and their sizes and types from the example object.
    App window, showing the inferred types and sizes of the length and width properties of the myRectangle object

Alternatively, you can provide the name of the value class, myRectangle, or a coder.ClassType object for that class. To define a coder.ClassType object, usecoder.typeof. For example:

  1. In MATLAB, define a coder.ClassType object that has the same properties asrect_obj.
    t = coder.typeof(rect_obj)
  2. In the app, provide t as the example.

To change the size or type of a property, click the field to the right of the property.

Consistency Between the Type Definition and Class Definition File

When you generate code, the properties that you define in the app must be consistent with the properties in the class definition file. If the class definition file has properties that your code does not use, your type definition in the app does not have to include those properties. The code generator removes properties that your code does not use.

Limitations for Using Objects as Entry-Point Function Inputs

Entry-point function inputs that are objects have these limitations:

See Also

coder.ClassType