Documentation Center

  • Trials
  • Product Updates

renamecats

Rename categories in categorical array

Syntax

  • B = renamecats(A,newnames) example
  • B = renamecats(A,oldnames,newnames) example

Description

example

B = renamecats(A,newnames) renames all the categories in the categorical array, A. Elements of B use the new category names.

example

B = renamecats(A,oldnames,newnames) renames only the categories specified by oldnames.

Examples

expand all

Rename All Categories

Create a categorical array containing states from New England.

A = categorical({'MA';'ME';'CT';'VT';'ME';'NH';'VT';'MA';'NH';'CT';'RI'})
A = 

     MA 
     ME 
     CT 
     VT 
     ME 
     NH 
     VT 
     MA 
     NH 
     CT 
     RI 

A is an 11-by-1 categorical array.

Display the categories of A.

categories(A)
ans = 

    'CT'
    'MA'
    'ME'
    'NH'
    'RI'
    'VT'

A has six categories.

Rename the categories to use the full state name instead of the abbreviation.

B = renamecats(A,{'Connecticut','Massachusetts',...
    'Maine','New Hampshire','Rhode Island' 'Vermont'})
B = 

     Massachusetts 
     Maine 
     Connecticut 
     Vermont 
     Maine 
     New Hampshire 
     Vermont 
     Massachusetts 
     New Hampshire 
     Connecticut 
     Rhode Island 

Elements of B use the new category names.

Display the categories of B.

categories(B)
ans = 

    'Connecticut'
    'Massachusetts'
    'Maine'
    'New Hampshire'
    'Rhode Island'
    'Vermont'

Rename One Category

Create a categorical array containing colors.

A = categorical({'red' 'blue'; 'purple' 'white'; 'green' 'red'})
A = 

     red         blue  
     purple      white 
     green       red   

A is a 3-by-2 categorical array.

Display the categories of A.

categories(A)
ans = 

    'blue'
    'green'
    'purple'
    'red'
    'white'

A has five categories that are listed in alphabetical order.

Change the category name from purple to violet.

B = renamecats(A,'purple','violet')
B = 

     red         blue  
     violet      white 
     green       red   

The element B(2,1) is violet instead of purple.

Display the categories of B.

categories(B)
ans = 

    'blue'
    'green'
    'violet'
    'red'
    'white'

violet replaces purple and the categories are no longer in alphabetical order. Note that the category has not changed its position.

Input Arguments

expand all

A — Categorical arrayvector | matrix | multidimensional array

Categorical array, specified as a vector, matrix, or multidimensional array.

newnames — New category names for Bstring | cell array of strings

New category names for B, specified as a string or a cell array of strings. The new category names must be unique, and must not duplicate any existing names.

oldnames — Old category names from Astring | cell array of strings

Old category names from A, specified as a string or a cell array of strings.

More About

expand all

Tips

  • Renaming categories does not change their position in categories(B). Use reordercats to change the category ordering. For example, you can use B = reordercats(B,sort(categories(B))) to put the categories in alphabetical order.

See Also

| | | | |

Was this topic helpful?