Documentation Center

  • Trial Software
  • Product Updates

isprotected

Determine whether categories of categorical array are protected

Syntax

Description

example

tf = isprotected(A) returns logical 1 (true) if the categories of A are protected. Otherwise, isprotected returns logical 0 (false).

  • true — When you assign new values to B, the values must belong to one of the existing categories. Therefore, you only can combine arrays that have the same categories. To add new categories to B, you must use the addcats function.

  • false — When you assign new values to B, the categories update automatically. Therefore, you can combine (nonordinal) categorical arrays that have different categories. The categories can update to include the categories from both arrays.

Examples

expand all

Determine Whether Categories Are Protected

Create a categorical array containing the sizes of 10 objects. Use the names small, medium, and large for the values 'S', 'M', and 'L'.

valueset = {'S','M','L'};
catnames = {'small','medium','large'};

A = categorical({'M';'L';'S';'S';'M';'L';'M';'L';'M';'S'},...
    valueset,catnames,'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'

Determine whether the categories of A are protected.

tf = isprotected(A)
tf =

     1

Since A is an ordinal categorical array, the categories are protected. If you try to add a new value that does not belong to one of the existing categories, for example A(11) = 'xlarge', then MATLAB® returns an error.

First, use addcats to add a new category for xlarge.

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

Since A is protected, you can now add a value for xlarge since it has an existing category.

A(11) = 'xlarge'
A = 

     medium 
     large 
     small 
     small 
     medium 
     large 
     medium 
     large 
     medium 
     small 
     xlarge 

A is now a 11-by-1 categorical array with four categories, such that small < medium < large < xlarge.

Input Arguments

expand all

A — Categorical arrayvector | matrix | multidimensional array

Categorical array, specified as a vector, matrix, or multidimensional array.

The categories of ordinal categorical arrays are always protected.

See Also

|

Was this topic helpful?