3

I have an array of structs in MATLAB, all of which have the same structure (same fields). I'd like a quick way to compile an array that contains all values of specific field from across the struct array. Is there any way doing this without using loops?

Thanks in advance

3

Suppose your array is named `a`

and you have a field `b`

. Accessing `a.b`

gives you a list of the values of `b`

fields for each element in `a`

. If you want to turn that into a list, just wrap the list in `[]`

. That is:

```
>> a = [struct('a', 1, 'b', 10, 'c', 100), struct('a', 2, 'b', 20, 'c', 200)];
>> a
a =
1x2 struct array with fields:
a
b
c
>> a.b
ans =
10
ans =
20
>> [a.b]
ans =
10 20
>> [a.c]
ans =
100 200
```

If you have a matrix of structs, you can use the above method to get a vector then reshape it into a matrix using:

```
>> reshape([a.b], size(a))
ans =
10 111
20 222
```

turn into a "vector/matrix" probably - yuk 2012-04-03 21:20