Select Page

MATLAB has several types of data structures in addition to the arrays, cell arrays, and structures that we have already seen. These can be found in the documentation under data types.

Categorical Arrays

Categorical arrays are a type of array that allows one to store a finite, countable number of different possible values. Categorical arrays are fairly new in MATLAB, introduced in R2013b. Categorical arrays are defined using the categorical function.
For example, a group is polled on their favorite ice cream flavors; the results are stored in a categorical array:

```>> icecreamfaves = categorical ({ 'Vanilla' , 'Chocolate' , ...
'Chocolate' , 'Rum Raisin' , 'Vanilla' , 'Strawberry' , .. .
'Chocolate' , 'Rocky Road' , 'Chocolate' , 'Rocky Road' , .. .
'Vanilla', 'Chocolate', 'Strawberry', 'Chocolate' });```

Another way to create this would be to store the strings in a cell array, and then convert using the categorical function:

```>> cellicecreamfaves = {'Vanilla', 'Chocolate', ...
'Chocolate', 'Rum Raisin', 'Vanilla', 'Strawberry', ... 'Chocolate',
'Rocky Road' , 'Chocolate' , 'Rocky Road' , ...
'Vanilla', 'Chocolate', 'Strawberry', 'Chocolate'}
>> icecreamfaves =categorical (cellicecreamfaves);```

There are several functions that can be used with categorical arrays. The function categories will return the list of possible categories as a cell column vector, sorted in alphabetical order.

```>>cats= categories (icecreamfaves)
cats=
'Chocolate'
'Rum Raisin'
'Strawberry'
'Vanilla'

```

The functions countcats and summary will show the number of occurrences of each of the categories.

```>> countcats (icecreamfaves)
ans=
6 2 1 2 3
>> summary ( icecreamfaves)
Chocolate RockyRoad RumRaisin Strawberry Vanilla
6                2         1          2       3

```

In the case of the favorite ice cream flavors, there is no natural order for them, so they are listed in alphabetical order. It is also possible to have ordinal categorical arrays, however, in which an order is given to the categories.
For example, a person has a wearable fitness tracker that tracks the days on which a personal goal for the number of steps taken is reached; these are stored in a file. To simulate this, a variable stepgoalsmet stores these data for a few weeks. Another cell array stores the possible days of the week.

```>>stepgoalsmet={'Tue', 'Thu', 'Sat', 'Sun', 'Tue', ...
'Sun' , 'Thu' , 'Sat', 'Wed', 'Sat', 'Sun'};
>> daynames = { 'Mon' , 'Tue' , 'Wed' , 'Thu' , 'Fri' , 'Sat' , 'Sun' } ;

```

Then, an ordinal categorical array, ordgoalsmet, is created. This allows days to be compared using relational operators.

```>> ordgoalsmet = categorical ( stepgoalsmet, daynames, 'Ordinal' , true) ;
>> summary(ordgoalsmet)
Mon Tue Wed Thu Fri Sat Sun
0   2   1   2   0   3   3
>> ordgoalsmet (1) < ordgoalsmet (3)
ans =
1
>> ordgoalsmet (4) < ordgoalsmet (3)
ans=
0

```