Documentation Center

  • Trial Software
  • Product Updates


Class: categorical

Label levels

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.


A = setlabels(A,labels)
A = setlabels(A,labels,levels)


A = setlabels(A,labels) labels the levels in the categorical array A using the cell array of strings or 2-D character matrix labels. Labels are assigned in the order given in labels.

A = setlabels(A,labels,levels) labels only the levels specified in the cell array of strings or 2-D character matrix levels.


Example 1

Relabel the species in Fisher's iris data using new categories:

load fisheriris
species = nominal(species);
species = mergelevels(...
species = setlabels(species,'hybrid','versicolor');
ans = 
    'hybrid'    'parent'

Example 2

  1. Load patient data from the CSV file hospital.dat and store the information in a dataset array with observation names given by the first column in the data (patient identification):

    patients = dataset('file','hospital.dat',...
  2. Make the {0,1}-valued variable smoke nominal, and change the labels to 'No' and 'Yes':

    patients.smoke = nominal(patients.smoke,{'No','Yes'});
  3. Add new levels to smoke as placeholders for more detailed histories of smokers:

    patients.smoke = addlevels(patients.smoke,...
                     {'0-5 Years','5-10 Years','LongTerm'});
  4. Assuming the nonsmokers have never smoked, relabel the 'No' level:

    patients.smoke = setlabels(patients.smoke,'Never','No');
  5. Drop the undifferentiated 'Yes' level from smoke:

    patients.smoke = droplevels(patients.smoke,'Yes');
    Warning: OLDLEVELS contains categorical levels that 
    were present in A, caused some array elements to have 
    undefined levels.

    Note that smokers now have an undefined level.

  6. Set each smoker to one of the new levels, by observation name:

    patients.smoke('YPL-320') = '5-10 Years';

See Also

Was this topic helpful?