Change Category Labels

This example shows how to change the labels for category levels in categorical arrays using setlabels. You also have the option to specify labels when creating a categorical array.

Load sample data.

The variable Cylinders contains the number of cylinders in 100 sample cars.

load('carsmall')
unique(Cylinders)
ans =

     4
     6
     8

The sample has 4-, 6-, and 8-cylinder cars.

Create an ordinal array.

Convert Cylinders to a nominal array with the default category labels (taken from the values in the data).

cyl  = ordinal(Cylinders);
getlabels(cyl)
ans = 

    '4'    '6'    '8'

ordinal created labels using the integer values in Cylinders, but you should provide labels for numeric data.

Change category labels.

Relabel the categories in cyl to Four, Six, and Eight.

cyl = setlabels(cyl ,{'Four','Six','Eight'});
getlabels(cyl)
ans = 

    'Four'    'Six'    'Eight'

Alternatively, you can specify category labels when you create a nominal or ordinal array using the second input argument, for example by specifying ordinal(Cylinders,{'Four','Six','Eight'}).

See Also

| | |

Related Examples

More About

Was this topic helpful?