# If you want to...

## Get values from inside an array of numbers

Indexing Cheatsheet

## Get values from within a cell array

use curly braces instead of parentheses

```C{1}  %gets the value in the first cell
C{1,2} %gets the value in the cell in the first row, second column
```

You can also get more than one element by using the colon operator:

```C{1:4,2} %returns a comma-separated list: C{1,2}, C{2,2}, C{3,2}, C{4,2}
C{:} % returns a comma-separated list of all values in C
```

## Get cells from within a cell array

Use parentheses. All normal indexing for arrays applies.

```C(1) %cell in position 1
C(1,2) %cell in first row, second column
```

In this case, using the colon operator returns a portion of the cell array, itself a cell array:

```C(1:5,2) %returns a 5x1 cell array
C(:) %returns the original cell array in column vector form
```

## Get data from within a structure

For a single (non-array) structure S with fields f and g, use the period operator:

```S.f
S.g
```

## Get data from within arrays of structures

For arrays of structures, we need to index the structure part of the expression:

```S(1).f %value of field f for structure in position 1
S(3,4).g %value of field g for structure in third row, fourth column
```

Using the colon operator on the structure array will get the values of a given field from multiple structures

```S(1:3).f %comma-separated list: S(1).f, S(2).f, S(3).f
S(1,3:4).g %comma-separated list: S(1,3).g, S(1,4).g
```

## Get data for all elements of an array of structures

The period operator returns a comma-separated list of the field values for all elements of the array of structures

```S.f %returns a comma-separated list: S(1).f, S(2).f, S(3).f, etc.
```

## Get data from a structure using a field name stored in a variable

If fname is a string variable containing the name of a field,

```S.(fname) %get value of field corresponding to string contained in fname
```

## Get data from multiple fields of multiple structures

```doc getfield
```

## Capture a comma-separated list in a vector

If the elements of the comma-separated list can be combined into a vector (i.e., are cells, structs, or numbers), we can use brackets:

```v = [a, b, c]; %vector of values in a, b, c
v = [C{1:5,2:4}]; %if cells of C contains numbers, v is a 15-element vector
v = [S.number]; %if number is a numerical field, v contains all number entries for struct array S
v = vertcat(S.number); %if number is a vector, we may need to use horzcat or vertcat to put it in a matrix
v = horzcat(S.number);
```

## Capture a comma-separated list in a cell array

Data of any type can be captured in a cell array. Given any comma-separated list, surrounding it with braces produces a cell array.

```c = {a, b, c}; %each cell contains a number
c = {C{1:5,2:5}}; %1x15 cell array; if we want to preserve the shape of C, we should use:
c = C(1:5,2:5); %same data as above, but shaped as a 5x3 cell array
c = {S.name}; %cell array of strings, if name is a string-type field and S a struct array
```

## Assign a comma-separated list to another comma-separated list

If we have two sequences of variables we'd like to assign, instead of

```a = alpha, b = beta, c = gamma;
```

we can use the deal function

```[a, b, c] = deal(alpha, beta, gamma);
```

For structures and cell arrays, we don't even need to use deal. If S and C are structure and cell arrays, respectively, each with exactly three elements,

```[a, b, c] = C{:};
[a, b, c] = S.number;
```

will work. This comes in even handier when we use a second comma-separated list on the left:

```[C{1:3}] = S(1:3).number
```

is equivalent to

```C{1}=S(1).number, C{2}=S(2).number, C{3}=S(3).number;
```

which is equivalent to

```C(1:3) = {S(1:3).number};
```

because the left-hand side and the right-hand side are both cell arrays of matching dimension.