GLG410/598--Computers in Earth and Space Exploration


Announcements Syllabus Schedule Weekly lecture notes Assignments Links

Cell Arrays

Let us go back a few steps and talk about data types. Matlab supports several different data types, or variable classes, that each store different kinds of data. Since Matlab is a higher level programming language, it uses these data types in the background to store your variables in an efficient way, so as to allocate memory usage in your computer appropriate for the kinds of data that you are working with. Matlab is doing all of this memory allocation for you, so you don't see "how the sausage is made".

Here is a list of the more important data types that Matlab supports:
ICON DATA TYPE DESCRIPTION
numeric integer or floating-point array (these are numeric values we are used to working with)
char characer array or strings (what we have been using to store text)
logical true/false array (remember our use of tf = x_locations <= xmax;, where tf is our logical array)
cell cell array (I will talk about this below)
struct structure array (we will not get to this, but it is EXTREMELY useful for more complex data storage)

Remember our numeric arrays were like this:
>>a = [9 10.2 3.45 4]

a =

    9    10.2    3.45    4

where we used the square brackets to define the array. Each space in the array is a NUMBER.

For cell arrays, each space is a CELL. And ANYTHING can be stuffed inside of a CELL. Let me show you some examples:
>>% Let us make our variable "a" into a cell array "a_cell"
>>a_cell = {9 10.2 3.45 4}

a_cell =

    [9]    [10.2]    [3.45]    [4]

Here we used curly brackets to define our cell array. Notice that each element of a_cell has square brackets around it. That is telling you that EACH NUMBER is it's own numeric array inside separate cells of the cell array a_cell.
>>% Let us try MIXING different data types and storing them in a cell array
>>b_cell = {'BK' 'ELFS' 40.618 -120.728}

b_cell =

    'BK'    'ELFS'    [40.618]    [-120.73]

In elements (cells) 1 and 2 of b_cell, we put character arrays, but in elements (cells) 3 and 4, we put numeric arrays.
NOTE: Each cell can contain arrays of different lengths and types. This makes it a very versatile data type.

Using Cell Arrays in Your Programming

2 Common Reasons to Use Cell Arrays
  1. store arrays of different lengths - text that doesn't always have the same number of characters, or vectors of different lengths
  2. store related information of different data types

Example - storing seismic station information

Network Code Station Code Latitude Longitude
BK MOD 41.902 -120.303
BK ELFS 40.618 -120.728
TA N02C 40.822 -123.306
TA O02C 40.177 -122.788

We would like to store all the relevant information about these seismic stations into one variable. This cell array will look and act EXACTLY like our table above.
>>StationInformation = {'BK' 'MOD' 41.902 -120.303
'BK' 'ELFS' 40.618 -120.728
'TA' 'N02C' 40.822 -123.306
'TA' 'O02C' 40.177 -122.788}

StationInformation = 

    'BK'    'MOD'     [41.902]    [ -120.3]
    'BK'    'ELFS'    [40.618]    [-120.73]
    'TA'    'N02C'    [40.822]    [-123.31]
    'TA'    'O02C'    [40.177]    [-122.79]

Now lets add a 5th column that includes 3 earthquakes that were recorded by each of these stations.
Earthquake Latitude Earthquake Longitude
46.2 153.3
-5.9 151
-8.5 157

Since these are the same data type (numeric) we can store these in a 3 row / 2 column matrix.
>>Earthquakes = [46.2 153.3
-5.9 151
-8.5 157]

Earthquakes =

     46.2        153.3
     -5.9          151
     -8.5          157

Now lets make a new cell array that includes the station information AND the 3 earthquake locations:
>>StationInformation = {'BK' 'MOD' 41.902 -120.303 Earthquakes
'BK' 'ELFS' 40.618 -120.728 Earthquakes
'TA' 'N02C' 40.822 -123.306 Earthquakes
'TA' 'O02C' 40.177 -122.788 Earthquakes}

StationInformation = 

    'BK'    'MOD'     [41.902]    [ -120.3]    [3x2 double]
    'BK'    'ELFS'    [40.618]    [-120.73]    [3x2 double]
    'TA'    'N02C'    [40.822]    [-123.31]    [3x2 double]
    'TA'    'O02C'    [40.177]    [-122.79]    [3x2 double]

Do you see how we were able to INSERT our earthquake location information into 1 CELL of the cell array StationInformation?

Referencing Elements in a Cell Array

We talked about array indexing in Lecture 3. For a cell array it works in a similar way EXCEPT for 2 rules:
  1. parentheses () - used to get individual cells from the cell array
  2. curly brackets {} - used to get contents of cells in the cell array
Let me show you what I mean. I'll first reference the cell array with ():
>>% Take our cell array "StationInformation" and get the 1st row, 2nd column cell
>>FirstStation = StationInformation(1,2)

FirstStation = 

    'MOD'

>>% "FirstStation" is a 1x1 cell array with contents 'MOD'
>>% I'll prove this is true by using "iscell"
>>iscell(FirstStation)

ans =

     1

>>% Now get the 1st row, 3nd column cell
>>MOD_lat = StationInformation(1,3)

MOD_lat = 

    [41.902]

>>% This is ALSO a cell array, even if it is just one number
>>iscell(MOD_lat)

ans =

     1

Sometimes this is not very useful. We want to be able to do some simple math or other operations which require a numeric or character array. We can use {} to get the contents OUT of a cell array:
>>% The same examples except using curly brackets
>>FirstStation = StationInformation{1,2}

FirstStation =

    MOD

>>% "FirstStation" is a character array
>>% I'll prove this is true by using "ischar"
>>ischar(FirstStation)

ans =

     1

>>% Now get the 1st row, 3nd column cell
>>MOD_lat = StationInformation{1,3}

MOD_lat =

    41.902

>>% This is NOW a numeric array
>>isnumeric(MOD_lat)

ans =

     1