From: Steven Lord on

"Walter Roberson" <roberson(a)hushmail.com> wrote in message
news:hkf4t6$idl$2(a)canopus.cc.umanitoba.ca...
> Steven Lord wrote:
>> "Arwel " <a.v.hughes(a)rl.ac.uk> wrote in message
>> news:hkf30s$oen$1(a)fred.mathworks.com...
>
>>> Suppose I have....
>
>>> a{1}.val = 1
>>> a{2}.val = 2
>>> a{3}.val = 3
>>>
>>> Is there a quick way of doing this without using a loop...
>>>
>>> for i = 1:3
>>> atot{i} = a{i}.val
>>> end
>
>> S = [a{:}];
>> atoi = {S.val};
>
> Unfortunately, that doesn't work, Steve.

I thought I'd tried this before I posted (once er twice er too many times
bitten, once more shy) so I double-checked and it does work (at least
assuming each cell of the variable a contains a struct array that is
compatible with the others with respect to concatenation.) In release
R2009b (and probably earlier, but I haven't checked):


>> a{1}.x = 1;
>> a{2}.x = 2;
>> a{3}.x = 3;
>> S = [a{:}]

S =

1x3 struct array with fields:
x

>> y = {S.x}

y =

[1] [2] [3]

>> whos y
Name Size Bytes Class Attributes

y 1x3 204 cell


> a{1} is a struct,

Correct.

> so S will be a 1 x 3 cell array of struct.

Incorrect. S is a regular 1-by-3 struct array created using a
comma-separated list of the cells of a, and S.x is also a comma-separated
list since S is a nonscalar struct array.

If I'd done "S = {a{:}}" you'd be correct.

> You cannot access a cell array of struct according to field name. Hence my
> cellfun solution.

True.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ