categories - List of categories in categorical array - MATLAB (original) (raw)
Main Content
List of categories in categorical array
Syntax
Description
C = categories([A](#bt0y41s-1-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.
C = categories([A](#bt0y41s-1-A),OutputType=[type](#mw%5Fc6efef70-31d4-4569-913b-fab593b94dd7))
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
Create a categorical array by using the categorical
function.
A = categorical(["plane" "car" "train" "car" "plane"])
A = 1×5 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 = 3×1 cell {'car' } {'plane'} {'train'}
Create a categorical array.
A = categorical(["plane" "car" "train" "car" "plane"])
A = 1×5 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 = 3×1 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 = 6×1 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 = 3×1 cell {'small' } {'medium'} {'large' }
Input Arguments
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 onA
, such assummary
andhistogram
. To change the order of the categories, usereordercats
.
Extended Capabilities
Thecategories
function supports tall arrays with the following usage notes and limitations:
OutputType
name-value argument is not supported.
For more information, see Tall Arrays.
Version History
Introduced in R2013b
When using the categories
function, you can specify theOutputType
name-value argument to return the list of categories as a categorical array or string array. By default, the function returns the list of categories as a cell array of character vectors.