renamecats - Rename categories in categorical array - MATLAB (original) (raw)
Main Content
Rename categories in categorical array
Syntax
Description
B = renamecats([A](#bt05mms-1-A),[newnames](#bt05mms-1-newnames))
renames categories in a categorical array. By default,renamecats
renames all categories. Elements ofB
use the new category names.
B = renamecats([A](#bt05mms-1-A),[oldnames](#bt05mms-1-oldnames),[newnames](#bt05mms-1-newnames))
renames only the categories specified by oldnames
.
Examples
Create a categorical array that has abbreviations for New England state names.
A = categorical(["MA";"ME";"CT";"VT";"ME";"NH";"VT";"MA";"NH";"CT";"RI"])
A = 11×1 categorical MA ME CT VT ME NH VT MA NH CT RI
Display the categories of A
.
ans = 6×1 cell {'CT'} {'MA'} {'ME'} {'NH'} {'RI'} {'VT'}
Rename the categories to full state names instead of abbreviations.
newnames = ["Connecticut" "Massachusetts" "Maine" "New Hampshire" "Rhode Island" "Vermont"]; B = renamecats(A,newnames)
B = 11×1 categorical Massachusetts Maine Connecticut Vermont Maine New Hampshire Vermont Massachusetts New Hampshire Connecticut Rhode Island
Display the categories of B
.
ans = 6×1 cell {'Connecticut' } {'Massachusetts'} {'Maine' } {'New Hampshire'} {'Rhode Island' } {'Vermont' }
Create a categorical array.
A = categorical(["purple" "blue" "purple" "red" "red" "blue"])
A = 1×6 categorical purple blue purple red red blue
Display its categories.
ans = 3×1 cell {'blue' } {'purple'} {'red' }
Change the category name purple
to violet
.
B = renamecats(A,"purple","violet")
B = 1×6 categorical violet blue violet red red blue
Display the new categories. Note that when violet
replaced purple
it did not change the order of the categories. They are no longer in alphabetical order.
ans = 3×1 cell {'blue' } {'violet'} {'red' }
Input Arguments
input array, specified as a categorical array.
New category names, specified as a string array, character vector, or cell array of character vectors. The new category names must be unique, and must not duplicate any existing names.
Old category names, specified as a string array, character vector, or cell array of character vectors.
Tips
- Renaming categories does not change their positions in
categories(B)
. Usereordercats
to change the category ordering. For example, you can useB = reordercats(B,sort(categories(B)))
to put the categories in alphabetical order.
Extended Capabilities
Therenamecats
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2013b