Main Content

Change Category Labels


The nominal and ordinal array data types are not recommended. To represent ordered and unordered discrete, nonnumeric data, use the Categorical Arrays data type instead.

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
ans = 3×1


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);
ans = 1x3 cell
    {'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'});
ans = 1x3 cell
    {'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