MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

large
medium
small


A is a 10-by-1 categorical array.


Display the categories of A.


categories(A)


ans = 3x1 cell array
{'small' }
{'medium'}
{'large' }


Verify That Categories Are Protected


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 = logical
1


The categories of A are protected.


Assign Value in 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. For example, you cannot assign the value 'xlarge' to
the categorical array, as in the expression A(2) = 'xlarge', because xlarge is not a
category of A. Instead, MATLAB® returns the error:


Error using categorical/subsasgn (line 68)


Cannot add a new category 'xlarge' to this categorical array


because its categories are protected. Use ADDCATS to


add the new category.


Work with Protected Categorical Arrays
Free download pdf