reordercats
Changes the order of the categories of a categorical array.
Category: Language Fundamentals > Data Types
Syntax
B = reordercats(A)
Sorts the categories alphabetically.
B = reordercats(A, neworder)
Uses the order given in neworder, a list of all the category names.
Description
The data are not modified; only the category list changes. The order matters for categories, sort, plots and tables that group by category, and for comparisons with < and > when the array is ordinal.
Input arguments
A- Categorical array.
neworder- Text or string array containing every category exactly once.
Output arguments
B- Categorical array with the same values and the new category order.
Example
c = categorical(["lo","hi","mid","lo"]);
c = reordercats(c, ["lo","mid","hi"]);
categories(c)
Output
ans = 3×1 cell
{'lo' }
{'mid'}
{'hi' }