# Introduction to Structures

## Why Structures?

Last class, we looked at cell arrays, which we considered using in a couple of common cases:

1. We need to store multiple types of data together in the same structure (as in a spreadsheet)
2. We need to store ragged data (i.e., vectors of different lengths) together in the same variable

But as we found out, cell arrays are pretty awkward to work with. More importantly, in lots of applications, we don't need something quite so generic. In fact, the following conditions often obtain:

1. We have a lot of data records we want to store together (i.e., subjects in an experiment, experimental sessions, etc.)
2. These records mix data types, but each record has the same variables

You might recognize these conditions as being similar to the organization of a spreadsheet or a database. They are also similar to concepts called structures or objects in other programming languages. In Matlab, they're called structs.

## An example: employee records

Let's start with a simple example. We want to make a variable, called employee, that stores different types of information about an employee (name, office, code, etc.) together:

```employee.name='Sue';
employee.office='31B';
employee.tags={'supervisor','logistics','Chapel Hill'};
employee.code=0375;
```

Note that if you type

```employee
```
```employee =

name: 'Sue'
office: '31B'
tags: {'supervisor'  'logistics'  'Chapel Hill'}
code: 375

```

Matlab will return the fields and their values. The second important thing to note is that we access the fields within a structure via the period (|.|) operator. The syntax is <structure>.<field>

## Practice

1. Try changing the name of the employee from Sue to Fran.
2. Add the tag certified to her record.

## Arrays of Structures

What's just as important is that, as with any data type of matching dimensions, we can have arrays of structures. For example, let's add a second employee to our existing structure to create an array:

```employee(2).name='Tim';
employee(2).office='32';
employee(2).tags={'trainee','sales'};
employee(2).code=5712;
```

## Practice

1. Add a third employee, Ralph.
2. Double-click to view this new record in the Variable Browser. What happens when we only assign a name to this new record?
3. What happens if, instead of making the office a string, as we have been, we assign Ralph's office as the number 5?

## Dealing with unknown structures

If we don't know in advance what fields a structure has (because we didn't make it, or perhaps a user supplied it at runtime), we can get around this by using fieldnames to get the names of its fields in a cell array and getfield to retrieve individual entries

```ff=fieldnames(employee) %return field names in a cell array
```
```ff =

'name'
'office'
'tags'
'code'

```
```getfield(employee(1),'code') %get a particular field of a particular array element
getfield(employee,ff{1}) %same thing, but using retrieved field name
```
```ans =

375

ans =

Sue

```

There are more complicated versions of these, in which we can pick individual records to get fields from, along with individual subparts of those fields. See the help for more info.

More simply, let's say we want to access some field of a struct, but we don't know exactly which one. For instance, the fieldname could be contained in a variable aa:

```aa='name';
employee(1).(aa)
```
```ans =

Sue

```

That is, we can replace the literal fieldname with the name of a variable in parentheses. Matlab replaces the parenthetical with the fieldname and evaluates the result.

## Challenge

Write code that, given an arbitrary structure

• gets and saves the names of its fields
• finds out how many elements it has (hint: numel)
• loops over each structure element and each field within that element, saving each field into a separate cell of a cell array
• each structure element should be a row, each field a column in the cell array

This code effectively converts a vector of structures into a cell array (cf struct2cell).