coder.ClassType - Represent set of MATLAB classes acceptable for input specification - MATLAB (original) (raw)
Main Content
Namespace: coder
Superclasses: coder.ArrayType
Represent set of MATLAB classes acceptable for input specification
Description
Objects of the coder.ClassType
specify value class objects that the generated code accepts. Use objects of this class only with the -args
option of the codegen
function. Do not pass as an input to a generated MEX function.
Creation
`t` = coder.typeof([classObject](#mw%5Fdfc011a4-ef3f-4aa3-820a-644483ead7d2))
creates a coder.ClassType
object forclassObject
.
`t` = coder.newtype([className](#mw%5F2577406b-8e6b-4160-a133-3710c9413c8d))
creates a coder.ClassType
object for an object of theclassName
class.
Input Arguments
Value class object for which to create the coder.ClassType
object. This input is an expression that evaluates to an object of a value class.
Name of a value class definition file on the MATLAB path specified as a character vector or string scalar.
Properties
When you create a coder.ClassType
objectt
by passing a value class object v
tocoder.typeof
, t
has same as the properties as v
with the Constant
attribute set tofalse
.
Similarly, when you create a coder.ClassType
objectt
by passing the name of the value class object, v
tocoder.newtype
, t
has same as the properties as v
with the Constant
attribute set tofalse
.
Examples
This example shows how to create a type object based on an example object in the workspace.
Create a value 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
Create a function that takes an object of myRectangle
as the input.
function z = getarea(r) %#codegen z = calcarea(r); end
Create an object of myRectangle
.
v = myRectangle with properties:
length: 1
width: 2
Create a coder.ClassType
object based on v
.
t =
coder.ClassType
1×1 myRectangle
Properties :
length : 1×1 double
width : 1×1 double
Edit Type Object
coder.typeof
creates a coder.ClassType
object that has the same properties names and types as v
.
Generate code for getarea
. Specify the input type by passing the coder.ClassType
object, t
, to the -args
option.
codegen getarea -args {t} -report
Code generation successful: View report
This example shows how to create a coder.ClassType
object for an object of the value class mySquare
by using coder.newtype
.
Create a value class mySquare
that has one property, side
.
classdef mySquare properties side; end methods function obj = mySquare(val) if nargin > 0 obj.side = val; end end function a = calcarea(obj) a = obj.side * obj.side; end end end
Create a coder.ClassType
type for mySquare
without assigning any property values.
t = coder.newtype('mySquare')
t = coder.ClassType 1×1 mySquare -- class with no properties
Edit Type Object
To ensure that t
has the properties of mySquare
, specify the type of side
by using t.Properties
.
t.Properties.side = coder.typeof(2)
t =
coder.ClassType
1×1 mySquare
Properties :
side : 1×1 double
Edit Type Object
Tips
- After you create a
coder.ClassType
, you can modify the types of the properties. For example, modify the type of theprop1
andprop2
properties of an objectt
:
t = coder.typeof(myClass)
t.Properties.prop1 = coder.typeof(int16(2));
t.Properties.prop2 = coder.typeof([1 2 3]); - After you create a
coder.ClassType
object, you can add properties. For example, add thenewprop1
andnewprop2
properties of an objectt
:
t = coder.typeof(myClass)
t.Properties.newprop1 = coder.typeof(int8(2));
t.Properties.newprop2 = coder.typeof([1 2 3]); - When you generate code, the properties of the
coder.ClassType
object that you pass to thecodegen
function 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, thecoder.ClassType
object does not have to include those properties. The code generator ignores properties that your code does not use.
Version History
Introduced in R2017a