Main Content

isprotected

Determine whether categories of categorical array are protected

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

collapse all

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 = 10x1 categorical
     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 = 3x1 cell
    {'small' }
    {'medium'}
    {'large' }

Determine whether the categories of A are protected.

tf = isprotected(A)
tf = logical
   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 an error is returned.

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 = 11x1 categorical
     medium 
     large 
     small 
     small 
     medium 
     large 
     medium 
     large 
     medium 
     small 
     xlarge 

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

Input Arguments

collapse all

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

The categories of ordinal categorical arrays are always protected.

Extended Capabilities

Version History

Introduced in R2013b