www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/DetermineWhetherCategoriesAreProtectedExample.m

    %% 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| is a 10-by-1 categorical array.  

%% 
% Display the categories of |A|. 
categories(A)  

%% 
% Determine whether the categories of |A| are protected. 
tf = isprotected(A) 

%%
% 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| is now a 11-by-1 categorical array with four categories, such that
% |small < medium < large < xlarge|.