categorical.addcats
categorical: B = addcats (A, newcats)
categorical: B = addcats (…, 'After', catname)
categorical: B = addcats (…, 'Before', catname)
Add categories to categorical array.
B = addcats (A, newcats) appends new categories
specified in newcats to the categorical array A at the end of
any existing categories. The output categorical array B does not
contain elements that belong to the newly added categories.
B = addcats (…, adds
the categories after the existing category specified by catname.
'After', catname)
B = addcats (…, adds
the categories before the existing category specified by catname.
'Before', catname)
catname must be either a character vector, a cellstr scalar or a
string scalar. newcats can be a string array, a cell array of
character vectors, or any type of array that can be converted to a cell
array of character vectors with the cellstr function, as long as
it does not contain any duplicate names or references existing category
in A.
When adding a single category, newcats can also be specified as a character vector.
Source Code: categorical
addcats adds new, initially-empty categories without changing any values — handy to reserve labels that may appear later. For an ordinal array place the new category with 'Before'/'After' so the order stays sensible.
C = categorical ({'S'; 'M'; 'S'}, {'S', 'M', 'L'}, 'Ordinal', true)
C =
3x1 categorical array
S
M
S
categories (C)
ans =
3x1 cell array
{'S'}
{'M'}
{'L'}
Reserve XL just after L.
categories (addcats (C, 'XL', 'After', 'L'))
ans =
4x1 cell array
{'S' }
{'M' }
{'L' }
{'XL'}