Define Categorical Array Inputs - MATLAB & Simulink (original) (raw)
Main Content
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
Use one of these procedures:
- Provide an Example Categorical Array Input
- Provide a Categorical Array Type
- Provide 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 usingcoder.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, usecoder.Constant
with the -args
option:
C = categorical({'r','g','b'}); codegen myFunction -args {coder.Constant(C)}
Define Categorical Array Inputs in the MATLAB Coder App
Use one of these procedures:
- Automatically Define Input Types by Using the App
- Define Input Parameter by Example by Using the App
- Specify Types of Entry-Point Inputs Using the App
Representation of Categorical Arrays
A coder type object for a categorical array describes the object and its properties. Usecoder.typeof or pass categorical
as a string scalar tocoder.newtype.
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. For example:
t = categorical({'r','g','b'}); tType = coder.typeof(t)
The representation of variable t
is stored in coder type objecttType
.
tType =
matlab.coder.type.CategoricalType 1x3 categorical Categories : 3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
If your workflow requires the legacy representation of coder type objects, use thegetCoderType
function on the variable that has the new representation of your class or object. See Legacy Representation of Coder Type Objects.
Resize Object Properties 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 a categorical
coder object, you can resize the object properties:
t = categorical({'r','g','b'}); tType = coder.typeof(t); tType.Categories = coder.resize(tType.Categories, [3 1],[1 0])
This code resizes the Categories
property to be upper-bounded at3
for the first dimension.
tType =
matlab.coder.type.CategoricalType 1x3 categorical Categories : :3x1 homogeneous cell Ordinal : 1x1 logical Protected : 1x1 logical
You can also resize the object by using coder.resize
. See Edit and Represent Coder Type Objects and Properties.
See Also
categorical | coder.Constant | coder.typeof