setcats

Sets the list of categories of a categorical array.

Category: Language Fundamentals > Data Types

Syntax

B = setcats(A, newcats)

Makes newcats the categories of A, in that order.

Description

This function replaces the whole category list in one step, so it can add, drop and reorder at once. For single operations, addcats, removecats and reordercats state the intent more clearly.

Input arguments

A
Categorical array.
newcats
Text or string array of category names.

Output arguments

B
Categorical array. Elements whose category is not in newcats become <undefined>; new names add unused categories.

Example

c = categorical(["a","b","a"]);
c2 = setcats(c, ["b","a","z"])
categories(c2)
Output
c2 = 1×3 categorical array

     a      b      a 
ans = 3×1 cell

    {'b'}
    {'a'}
    {'z'}

See also

addcats removecats reordercats renamecats categorical