Specify Value Class Objects as Inputs
You can specify that an input variable is an instance of a value class by using the
MATLAB®
Coder™ app or by using the codegen
command at the command line. In some
cases, if the input variable is a user-written MATLAB class that contains property validation, you can use function argument
validation to specify the input type. See Use User-Written MATLAB Classes in arguments Blocks. Code generation does
not support handle class inputs to entry-point functions.
To specify a value class as an input type, the class definition must be in the working directory or on the MATLAB path.
If you generate code by using the MATLAB Coder, you can define the value class input automatically or manually. When you choose to define a value class input manually, the app prompts you to enter a MATLAB command that generates an instance of the value class.
If you generate code at the command line by using the
codegen
command, create an instance of the value class and pass this object tocodegen
by using the-args
option.
Alternatively, you can create a type for an object of the value class at the command line
by using coder.typeof
or interactively by using the
Coder Type Editor. See Create and Edit Input Types by Using the Coder Type Editor.
Example: Generate Code for an Entry-Point Function That Takes a Value Class Input
Consider the entry-point function calcArea
, which takes an instance
of the value class MyRectangle
and multiplies the
length
and width
properties of the
MyRectangle
object.
function out = calcArea(r) %#codegen out = r.length*r.width; end
Assume that the value class MyRectangle
is defined in the working
directory or on the MATLAB path.
classdef MyRectangle properties length (1,1) double width (1,1) double end end
Generate Code by Using the MATLAB Coder App
Define calcArea
as an entry-point function. Open the
Entry Points pane and click Click to define data
type next to input variable r
. Type
MyRectangle
in the class definition box. The app prompts you
to enter MATLAB code that produces a MyRectangle
object. At this
prompt, enter MyRectangle
. The app determines the types of the
length
and width
properties based on the
class definition.
To generate code, click Generate Code in the MATLAB Coder toolstrip or in the Output section of the code generation sidebar.
Generate Code by Using the codegen
command
Create an instance of the MyRectangle
class at the command line
and pass this instance to the codegen
command.
myObj = MyRectangle
myObj = MyRectangle with properties: length: 0 width: 0
codegen calcArea -args {myObj} -report
Code generation successful: View report
Open the report and click the Variables tab. The input
argument r
has the same properties as the
myObj
object.
Consistency Between Type Definitions and Class Definition
When you generate code, the properties you define in the app or the properties of the
object that you pass to the codegen
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, the type definition does not have to include
those properties. The code generator removes properties that you do 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.