Main Content

renamecats

Rename categories in categorical array

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

collapse all

Create a categorical array containing states from New England.

A = categorical({'MA';'ME';'CT';'VT';'ME';'NH';'VT';'MA';'NH';'CT';'RI'})
A = 11x1 categorical
     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 = 6x1 cell
    {'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 = 11x1 categorical
     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 = 6x1 cell
    {'Connecticut'  }
    {'Massachusetts'}
    {'Maine'        }
    {'New Hampshire'}
    {'Rhode Island' }
    {'Vermont'      }

Create a categorical array containing colors.

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

A is a 3-by-2 categorical array.

Display the categories of A.

categories(A)
ans = 5x1 cell
    {'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 = 3x2 categorical
     red         blue  
     violet      white 
     green       red   

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

Display the categories of B.

categories(B)
ans = 5x1 cell
    {'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

collapse all

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

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

Old category names from A, specified as a character vector, a cell array of character vectors, or a string array.

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.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2013b