Define Categorical Array Inputs - MATLAB & Simulink (original) (raw)
You can define categorical array inputs at the command line or in theMATLAB® Coder™ app. Code generation does not support the programmatic specification ofcategorical
input types by using function argument validation (arguments blocks) or by using preconditioning (assert statements).
Define Categorical Array Inputs at the Command Line
Define categorical array inputs at the command line by providing an example input or by using a categorical
coder type. You can also specify a constant categorical array input. Alternatively, if you have a test file that calls your entry-point function with example inputs, you can determine the input types by using coder.getArgTypes.
Provide an Example Categorical Array Input
Use the -args
option:
C = categorical({'r','g','b'}); codegen myFunction -args {C}
Provide a Categorical Array Type
To provide a type for a categorical array to codegen
:
- Define a categorical array. For example:
C = categorical({'r','g','b'}); - Create a type from
C
. - Pass the type to
codegen
by using the-args
option.
codegen myFunction -args {t}
Provide a Constant Categorical Array Input
To specify that a categorical array input is constant, use coder.Constant with the -args
option:
C = categorical({'r','g','b'}); codegen myFunction -args {coder.Constant(C)}
Representation of Categorical Arrays
The coder type object displays a succinct description of the object properties while excluding internal state values. The command line interface displays the type and size of nonconstant properties and the values of constant properties. For example, create a codercategorical
type with a size of 3-by-1.
c = categorical({'r','g','b'}); cType = coder.typeof(c);
The representation of variable c
is stored in coder type objectcType
.
cType =
matlab.coder.type.CategoricalType 1x3 categorical Categories : 3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
If necessary, you can obtain the legacy coder.ClassType
representation of a categorical
coder type by using the methodgetCoderType
. For example, to view the underlyingcoder.ClassType
representation of the cType
object, use this command:
ans =
coder.ClassType
1×1 categorical
Properties :
codes : :10×1 uint8
categoryNames : :3×1 locked homogeneous cell
base: 1×1 char
isProtected : 1×1 logical
isOrdinal : 1×1 logical
numCategoriesUpperBound : 1×1 double
Object Properties
You can edit the properties of coder categorical
type objects. You can assign scalar values to object properties. Values are implicitly converted to the corresponding coder type values when they are assigned to coder type object properties. You can resize objects themselves by using the coder.resize function or by editing object properties directly.
Resize Object Properties by Using coder.resize
You can resize categorical
objects and object properties by usingcoder.resize
. You can also create arrays within properties.
For example, create a coder categorical
type with a size of 3-by-1.
c = categorical({'r','g','b'}); cType = coder.typeof(c)
cType =
matlab.coder.type.CategoricalType 1x3 categorical Categories : 3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
Use coder.resize
to make Categories
variable-length with an upper bound of 3.
cType.Categories = coder.resize(cType.Categories,[3 1],[true false])
cType =
matlab.coder.type.CategoricalType 1x3 categorical Categories : :3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
Resize Objects Directly
You can also resize certain type objects themselves by editing the object properties. For example, to change the number of rows in the cType
object, edit theSize
property.
cType =
matlab.coder.type.CategoricalType 10x1 categorical Categories : :3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
You can also make the number of rows variable size by using theVarDims
property.
cType.VarDims = [true false]
cType =
matlab.coder.type.CategoricalType :10x1 categorical Categories : :3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
See Also
categorical | coder.Constant | coder.typeof | coder.newtype