Main Content

categories

List of categories in categorical array

Description

example

C = categories(A) returns a list of the categories in the categorical array A. The output is a cell array of character vectors.

The output lists all categories in A, including categories that are not present in any element of A. To return a list that includes only the categories that are present in elements of A, use the unique function.

example

C = categories(A,OutputType=type) specifies the output type. You can return the list of categories as a cell array of character vectors, categorical array, or string array. (since R2024a)

Examples

collapse all

Create a categorical array by using the categorical function.

A = categorical(["plane","car","train","car","plane"])
A = 1x5 categorical
     plane      car      train      car      plane 

To return a list of the categories in A, use the categories function. The order of the categories was determined when you created A. If you do not specify an order when you use categorical, then categorical calls the unique function to determine the order of the categories from the input array.

C = categories(A)
C = 3x1 cell
    {'car'  }
    {'plane'}
    {'train'}

Create a categorical array.

A = categorical(["plane","car","train","car","plane"])
A = 1x5 categorical
     plane      car      train      car      plane 

To return the list of categories as a categorical array, specify the OutputType name-value argument.

C = categories(A,OutputType="categorical")
C = 3x1 categorical
     car 
     plane 
     train 

Create an ordinal categorical array. Specify the order of the categories as the mathematical ordering small < medium < large.

A = categorical(["medium";"large";"small";"small";"small";"large"], ...
                ["small","medium","large"], ...
                Ordinal=true)
A = 6x1 categorical
     medium 
     large 
     small 
     small 
     small 
     large 

Return a list of the categories in the ordinal categorical array. The categories appear in the order you specified when you created the categorical array.

C = categories(A)
C = 3x1 cell
    {'small' }
    {'medium'}
    {'large' }

Input Arguments

collapse all

Input array, specified as a categorical array.

Output data type, specified as one of these options:

  • "char" — Return a cell array of character vectors. If you specify "char", then the output is equivalent to the output of the first syntax.

  • "string" — Return a string array.

  • "categorical" — Return a categorical array.

Tips

  • The order of the categories listed in C is the same order used by functions that you can call on A, such as summary and histogram. To change the order of the categories, use reordercats.

Extended Capabilities

Version History

Introduced in R2013b

expand all