6.4) The Cell Class
Cell arrays are very flexible arrays that can hold:
- values of different types - e.g. numeric and char data in different cells, or:
- sub-arrays of different sizes - and hence:
- multiple char vectors of different lengths (e.g. words and phrases).
So they typically store heterogeneous data or text strings (remember that a single text string is a char vector).
Cell arrays are indexed much like other arrays. However:
- We normally create a cell array using curly brackets. Then:
- If we access cell array elements using round brackets, we end up with a cell array containing the selected data. i.e., we end up with a subset of the array.
- To access a data value itself we must use curly brackets { }. This means we pull out the data within a cell container rather than the cell container itself. For example:
[matlab]
>> Days={'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'};
>> First_Day=Days{1};
>> First_Letter_Of_3rd_Day=Days{3}(1);
% Note the use of { } and ( ). Days{3}='Wed'
[/matlab] - Of course, even if you want to store single-letter strings within an otherwise numeric array, you still need a cell array.
Cell arrays are used when elements of different types or sizes need to be stored in a single array. For greater flexibility (e.g. to combine multiple arrays as a single entity), you should use structures instead.
We will return to cell arrays later.