Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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

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 = 

     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 =

  6×1 cell array

    '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 =

  6×1 cell array

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

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 =

  5×1 cell array

    '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 =

  5×1 cell array

    '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 or a cell array of character vectors. The new category names must be unique, and must not duplicate any existing names.

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

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

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.

Introduced in R2013b

Was this topic helpful?