Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

This example shows how to work with a categorical array with protected categories.

When you create a categorical array with the function `categorical`

,
you have the option of specifying whether or not the categories are
protected. Ordinal categorical arrays always have protected categories,
but you also can create a nonordinal categorical array that is protected
using the `'Protected',true`

name-value pair argument.

When you assign values that are not in the array's list of categories, the array updates automatically so that its list of categories includes the new values. Similarly, you can combine (nonordinal) categorical arrays that have different categories. The categories in the result include the categories from both arrays.

When you assign new values to a *protected* categorical
array, the values must belong to one of the existing categories. Similarly,
you can only combine protected arrays that have the same categories.

If you want to combine two nonordinal categorical arrays that have protected categories, they must have the same categories, but the order does not matter. The resulting categorical array uses the category order from the first array.

If you want to combine two ordinal categorical array (that always have protected categories), they must have the same categories, including their order.

To add new categories to the array, you must use the
function `addcats`

.

Create a categorical array containing the sizes of 10 objects.
Use the names `small`

, `medium`

,
and `large`

for the values` 'S'`

, `'M'`

,
and `'L'`

.

A = categorical({'M';'L';'S';'S';'M';'L';'M';'L';'M';'S'},... {'S','M','L'},{'small','medium','large'},'Ordinal',true)

A = medium large small small medium large medium large medium small

`A`

is a 10-by-1 categorical array.

Display the categories of `A`

.

categories(A)

ans = 'small' 'medium' 'large'

When you create an ordinal categorical array, the categories are always protected.

Use the `isprotected`

function to verify that
the categories of `A`

are protected.

tf = isprotected(A)

tf = 1

The categories of `A`

are protected.

Try to add the value `'xlarge'`

to the categorical
array, `A`

.

`A(2) = 'xlarge'`

Error using categorical/subsasgn (line 55) Cannot add a new category 'xlarge' to this categorical array because its categories are protected. Use ADDCATS to add the new category.

If you try to assign a new value, that does not belong to one
of the existing categories, then MATLAB^{®} returns an error.

Use `addcats`

to add a new category for `xlarge`

.
Since `A`

is ordinal you must specify the order for
the new category.

A = addcats(A,'xlarge','After','large');

Now, you assign a value for `'xlarge'`

,
since it has an existing category.

`A(2) = 'xlarge'`

A = medium xlarge small small medium large medium large medium small

`A`

is now a 10-by-1 categorical array with
four categories, such that ```
small < medium < large <
xlarge
```

.

Create another ordinal categorical array, `B`

,
containing the sizes of five items.

B = categorical([2;1;1;2;2],1:2,{'xsmall','small'},'Ordinal',true)

B = small xsmall xsmall small small

`B`

is a 5-by-1 categorical array with two
categories such that `xsmall < small`

.

To combine two ordinal categorical arrays (which always have protected categories), they must have the same categories and the categories must be in the same order.

Add the category `'xsmall'`

to `A`

before
the category `'small'`

.

A = addcats(A,'xsmall','Before','small'); categories(A)

ans = 'xsmall' 'small' 'medium' 'large' 'xlarge'

Add the categories `{'medium','large','xlarge'}`

to `B`

after
the category `'small'`

.

B = addcats(B,{'medium','large','xlarge'},'After','small'); categories(B)

ans = 'xsmall' 'small' 'medium' 'large' 'xlarge'

The categories of `A`

and `B`

are
now the same including their order.

Vertically concatenate `A`

and `B`

.

C = [A;B]

C = medium large small small medium large medium large medium small xlarge small xsmall xsmall small small

The values from `B`

are appended to the values
from `A`

.

List the categories of `C`

.

categories(C)

ans = 'xsmall' 'small' 'medium' 'large' 'xlarge'

`C`

is a 16-by-1 ordinal categorical array
with five categories, such that ```
xsmall < small < medium
< large < xlarge
```

.

`addcats`

| `categorical`

| `categories`

| `isordinal`

| `isprotected`

| `summary`

- Create Categorical Arrays
- Convert Text in Table Variables to Categorical
- Access Data Using Categorical Arrays
- Combine Categorical Arrays
- Combine Categorical Arrays Using Multiplication

Was this topic helpful?