removecats

Removes categories from a categorical array.

Category: Language Fundamentals > Data Types

Syntax

B = removecats(A)

Removes all categories that are not used.

B = removecats(A, oldcats)

Removes the named categories.

Description

The category list is separate from the data. removecats deletes names from that list, and any element that had such a value is turned into an undefined element rather than deleted. Call it without a second argument to tidy up after filtering, when some categories no longer occur.

Input arguments

A
Categorical array.
oldcats
Category names to remove (text or string array).

Output arguments

B
Categorical array; elements that belonged to a removed category become <undefined>.

Example

c = categorical(["a","b","a","c"]);
c2 = removecats(c, "b")
categories(c2)
Output
c2 = 1×4 categorical array

     a      <undefined>      a      c 
ans = 2×1 cell

    {'a'}
    {'c'}

See also

categorical addcats renamecats reordercats categories mergecats