%% How to Create Ordinal Categorical Arrays
% This example shows how to create an ordinal categorical array using the categorical function with the 'Ordinal',true name-value pair argument.
%% Ordinal Categorical Array from a Cell Array of Character Vectors
% Create an ordinal categorical array, sizes, from a cell array of character vectors, A. Use valueset, specified as a vector of unique values, to define the categories for sizes.

A = {'medium' 'large';'small' 'medium'; 'large' 'small'};
valueset = {'small', 'medium', 'large'};

sizes = categorical(A,valueset,'Ordinal',true)

% sizes is 3-by-2 ordinal categorical array with three categories such that small < medium < large. The order of the values in valueset becomes the order of the categories of sizes.
%
%% Ordinal Categorical Array from Integers
% Create an equivalent categorical array from an array of integers. Use the values 1, 2, and 3 to define the categories small, medium, and large, respectively.

A2 = [2 3; 1 2; 3 1];
valueset = 1:3;
catnames = {'small','medium','large'};

sizes2 = categorical(A2,valueset,catnames,'Ordinal',true)

%
% Compare sizes and sizes2

isequal(sizes,sizes2)

% sizes and sizes2 are equivalent categorical arrays with the same ordering of categories.
%
%% Convert a Categorical Array from Nonordinal to Ordinal
% Create a nonordinal categorical array from the cell array of character vectors, A.

sizes3 = categorical(A)

%
% Determine if the categorical array is ordinal.

isordinal(sizes3)

% sizes3 is a nonordinal categorical array with three categories, {'large','medium','small'}. The categories of sizes3 are the sorted unique values from A. You must use the input argument, valueset, to specify a different category order.
%
% Convert sizes3 to an ordinal categorical array, such that small < medium < large.

sizes3 = categorical(sizes3,{'small','medium','large'},'Ordinal',true);

% sizes3 is now a 3-by-2 ordinal categorical array equivalent to sizes and sizes2.
% Copyright 2012 The MathWorks, Inc.