Main Content

Define Categorical Array Inputs

You can define categorical array inputs at the command line or in the MATLAB® Coder™ app. Code generation does not support the programmatic specification of categorical 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:

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:

  1. Define a categorical array. For example:

    C = categorical({'r','g','b'});
    

  2. Create a type from C.

    t = coder.typeof(C);
    

  3. 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)}

Define Categorical Array Inputs in the MATLAB Coder App

Use one of these procedures:

Representation of Categorical Arrays

A coder type object for a categorical array describes the object and its properties. Use coder.typeof or pass categorical as a string scalar to coder.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 object tType.

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 the getCoderType 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 at 3 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

| |

Related Topics