categorical

Methods

Method Reference: categorical.setcats

categorical: B = setcats (A, newcats)

Set categories in categorical array.

B = setcats (A, newcats) sets categories in the categorical array B according to the elements of the input array A and the categories specified by newcats according to the following rules:

  • Any element of A that corresponds to a category listed in newcats is copied to B with the same categorical value.
  • Any categories of A not listed in newcats are not copied to B and the corresponding elements of B are undefined.
  • New categories listed in newcats that are not present in A are added in B, but without any elements equal to these new categories.

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. When setting a single new category, newcats can also be specified as a character vector.

Source Code: categorical

Example: 1

setcats replaces the whole category list with the one you give: values that fall outside the new set become <undefined>, and the categories end up as exactly the names you specified (in that order).

 C = categorical ({'S'; 'M'; 'L'; 'M'}, {'S', 'M', 'L'})
C =
  4x1 categorical array

    S    
    M    
    L    
    M

Keep only S and M; the L element is dropped to <undefined>.

 setcats (C, {'S', 'M'})
ans =
  4x1 categorical array

    S              
    M              
    <undefined>    
    M