Main Content

isprotected

Determine if categories of categorical array are protected

Description

tf = isprotected(A) returns logical 1 (true) if the categories of a categorical array 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.

example

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 

Display the categories of A.

categories(A)
ans = 3x1 cell
    {'small' }
    {'medium'}
    {'large' }

Determine if the categories of A are protected.

tf = isprotected(A)
tf = logical
   1

Because 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");

You can add a value for xlarge because the category now exists.

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

Input array, specified as a categorical array.

The categories of ordinal categorical arrays are always protected.

Extended Capabilities

Version History

Introduced in R2013b