| Statistics Toolbox™ | ![]() |
B = addlevels(A,newlevels)
B = addlevels(A,newlevels) adds new levels to the categorical array A. newlevels is a cell array of strings or a two-dimensional character matrix that specifies the levels to be added. addlevels adds the new levels at the end of the list of possible categorical levels in A, but does not modify the value of any element. B does not contain elements at the new levels.
Add levels for additional species in Fisher's iris data:
load fisheriris
species = nominal(species,...
{'Species1','Species2','Species3'},...
{'setosa','versicolor','virginica'});
species = addlevels(species,{'Species4','Species5'});
getlabels(species)
ans =
'Species1' 'Species2' 'Species3' 'Species4' 'Species5'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',...
'delimiter',',',...
'ReadObsNames',true);
Make the {0,1}-valued variable smoke nominal, and change the labels to 'No' and 'Yes':
patients.smoke = nominal(patients.smoke,{'No','Yes'});
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'});
Assuming the nonsmokers have never smoked, relabel the 'No' level:
patients.smoke = setlabels(patients.smoke,'Never','No');
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.
Set each smoker to one of the new levels, by observation name:
patients.smoke('YPL-320') = '5-10 Years';droplevels, islevel, mergelevels, reorderlevels, getlabels
![]() | addedvarplot | andrewsplot | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |