Lecture 7

Chapter 8 - Data Structures - Cell Arrays and Structures

Synopsis
In past lectures, we've covered data structures such arrays. In this lecture, we will introduce two more data structures from Chapter 8 - Data Structures: cell arrays and structures. The topics that will be covered include initialization, reference, modifying, and nesting of cell arrays and structures. In addition to discussing the content form Chapter 8, we will also go over the details for the Final Projects


Daily Quiz

Quiz 12


Final Projects

Details


Data Structures

Data structures are something we've seen before. They are variables that hold more than one value, and include arrays (i.e. vectors and matrices). Arrays are very useful, but require that their elements be of the same type. Fortunately, MATLAB has two other data structures to provide a bit more flexibility.

Cell Arrays

The first new data structure that we will look at are cell arrays. In many ways, cell arrays can be thought of as vectors and matrices that can hold different types. In fact, cell arrays actually hold pointer instead of actual values. Cell arrays can be initialized in two different ways: using the cell function or using {}.

1
2
3
4
5
% initialize a 2x2 cell array with cell function
ca1 = cell(2,2)

% initialize a 1x3 cell array with {}
ca2 = {4,'the',1:3}
ca1 = 

    []    []
    []    []


ca2 = 

    [4]    'the'    [1x3 double]

As you can see, the cell function creates an empty cell array of a given size; whereas, the {} produce a cell array with content. The contents of each element of the cell array are displayed in a new format. Elements that are themselves data structures will appear as a summary that describes the dimensions and type (ex. [1x3 double]).

Referencing elements of a cell array is also very similar to referencing vectors and matrices; however, there are two different modes. We can reference the contents of a cell using {} or the entire cell using ().

1
2
3
4
5
% reference contents
ca2{2}

% reference cell
ca2(2)
ans =

the


ans = 

    'the'

The different between referencing the contents and the cell itself can be seen when referencing multiple elements at once. If we use content indexing, we get back comma separated list. If we use cell referencing, we get back a new cell array.

1
2
3
4
5
% reference multiple cell contents
ca2{1:2}

% reference multiple cells
newca = ca2(1:2)
ans =

     4


ans =

the


newca = 

    [4]    'the'

Structures

Structures are a type of data structure that groups related information as fields. The can be thought of as a list of properties related to some physical or abstract item. Unlike arrays and cell arrays, structures cannot be index. Rather, they are referenced by their fields. We can initialize a structure by using the struct function or the dot operator.

1
2
3
4
5
6
7
% initialize a structure with struct function
cart1 = struct('user', 'Jane', 'userid', 103, 'items', 4)

% initialize a structure with dot operator
cart2.user = 'Jane';
cart2.userid = 103;
cart2.items = 4
cart1 = 

      user: 'Jane'
    userid: 103
     items: 4


cart2 = 

      user: 'Jane'
    userid: 103
     items: 4

As you can see, structures can be anything that has a set of related properties. If we would like to remove a property or field we can use the rmfield function.

1
2
% remove userid
cart2 = rmfield(cart2,'userid')
cart2 = 

     user: 'Jane'
    items: 4

We mentioned earlier that we cannot index a structure, but we can reference the contents of a field using the dot operator.

1
2
% reference user
cart2.user
ans =

Jane

What if we don't know the fields of a structure? Well, we can access them using the fieldnames function. The fieldnames function returns a cell array of all fields of the given structure. This can be particularly useful for sequentially accessing structure fields.

Data Structures of Data Structures

Now that we have more than one type of data structure, you might start seeing code that incorporates several data structures into one. For instance, you can create vectors of structures or structures whose fields are structures. Nesting of data structures is useful for handling more complex types of data format.


Final Words

Today we took a quick peak at two new data structures. Cell arrays and structures will allow us to handle much more complex data manipulation than we could previously. We will see the value of using cell arrays, structures, and nested data structures in the next lab.