Edit and Represent Coder Type Objects and Properties
Passing an object to coder.typeof
or passing a class name as a string
scalar to coder.newtype
creates an object that represents
the type of object for code generation.
The coder type object displays a succinct description of the object properties while excluding internal state values. Nonconstant properties display their type and size, while constant properties display only their values.
To create a coder type object, pass a compatible object to coder.typeof
. For example:
t = categorical({'r','g','b'}); tType = coder.typeof(t)
The representation of variable t
is stored in coder type object
tType
.
tType = matlab.coder.type.CategoricalType 1x3 categorical Categories : 3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
Object Properties
You can edit the properties of coder type objects. You can assign scalar values to the
object properties. Values are implicitly converted to the corresponding coder type values
when they are assigned to coder type object properties. The code generator implicitly
converts constants assigned to coder type object properties to
coder.Constant
values. You can resize objects themselves
Resize Objects by Using coder.resize
You can resize most objects by using coder.resize
. You can resize objects, its properties and create arrays
within the properties.
For example, for a timetable
coder object, you can resize the
object:
t = timetable((1:5)',(11:15)','SampleRate',1);
tType = coder.typeof(t);
tType = coder.resize(tType, [10 2],[1 0])
This code resizes the timetable
to a :10x2
object.
tType = matlab.coder.type.RegularTimetableType :10x2 timetable Data : 1x2 homogeneous cell Description : 1x0 char UserData : 0x0 double DimensionNames : {'Time'} {'Variables'} VariableNames : {'Var1'} {'Var2'} VariableDescriptions : 1x2 homogeneous cell VariableUnits : 1x2 homogeneous cell VariableContinuity : 1x2 matlab.internal.coder.tabular.Continuity StartTime : 1x1 matlab.coder.type.DurationType SampleRate : 1x1 double TimeStep : 1x1 matlab.coder.type.DurationType
The constant properties of tType
display their values. The
nonconstant properties display only their type and size.
Note
Not all types representing MATLAB® classes are compatible with coder.resize
.
Resize Objects by Editing Object Properties
You can resize the objects by editing the properties themselves. For example, to
change the size of duration
coder type object x
,
edit the Size
property.
x = coder.typeof(duration((1:3),0,0)); x.Size = [10 10]
This code changes the size of the coder type object.
x = matlab.coder.type.DurationType 10x10 duration Format : 1x8 char
You can also make the coder type object variable-size by setting the
VarDims
flag:
x.VarDims(2) = true
The second dimension of the coder type object is upper-bounded at
10
.
x = matlab.coder.type.DurationType 10x:10 duration Format : 1x8 char
Legacy Representation of Coder Type Objects
Starting in R2021a, calling coder.typeof
no longer returns a coder.ClassType
object. If your workflow requires the legacy representation of coder type objects, use the
getCoderType
function on the variable that has the new representation
of your class or object. For example, to get the legacy representation of a
datetime
variable, use the variable that has the new representation
tt
to call the getCoderType
function:
t = datetime; tt = coder.typeof(t); ttLegacy = tt.getCoderType()
In the Coder Type Editor, the code generator includes the function
getCoderType
for coder type objects. Use this function to return the
legacy representation of coder types. See, Create and Edit Input Types by Using the Coder Type Editor
Certain MATLAB data types provide customized type representations for MATLAB code generation. In other cases, the type is represented using coder.ClassType
.
See Also
coder.resize
| coder.newtype
| coder.typeof
| Code Generation for Variable-Size Arrays