Specify Objects as Inputs in the MATLAB Coder App
In the MATLAB® Coder™ app, to specify the type of an input that is a value class object:
Define the value class. For example, define a class
myRectangle
.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
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
In the app, create a project for
getarea
. On the Define Input Types page, specify the type of the object in one of these ways:
Automatically Define an Object Input Type
Write a test file
getarea_test
that creates an object of themyRectangle
class and passes it togetarea
. For example:rect_obj = myRectangle(4,5); rect_area = getarea(rect_obj); disp(rect_area);
In the app, on the Define Input Types page, specify the test file
getarea_test
.Click Autodefine Input Types.
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.
In MATLAB, define an object of the value class
myRectangle
.rect_obj = myRectangle(4,5)
In the app, on the Define Input Types page, click Let me enter input or global types directly.
Click the field to the right of the input parameter
r
.Select Define by Example.
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.
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, use
coder.typeof
. For example:
In MATLAB, define a
coder.ClassType
object that has the same properties asrect_obj
.t = coder.typeof(rect_obj)
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:
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, see Subclassing Multiple Classes.