From: Nick Evans on
Is there a way to access multiple structure fields without using a loop?

s=struct('a',2,'b',4,'c',6)
fn = {'a' 'c'}

for i=1:numel(fn) % This loop seems superfluous
a(i)=s.(fn{i});
end
a % returns [2 6]

The expected solution a = s.(fn) doesn't work. This seems like an oversight in MATLAB's implementation of dynamic field references. Or can someone explain why this would be a bad idea?
From: Matt J on
"Nick Evans" <cheesecake23(a)gmail.com> wrote in message <i2h79n$j9p$1(a)fred.mathworks.com>...

> The expected solution a = s.(fn) doesn't work. This seems like an oversight in MATLAB's implementation of dynamic field references. Or can someone explain why this would be a bad idea?
============

I agree that it would be a good feature to add. For now, you can abbreivate the for-loop as follows,

a= cellfun(@(f) s.(f), fn);
From: Walter Roberson on
Nick Evans wrote:
> Is there a way to access multiple structure fields without using a loop?
>
> s=struct('a',2,'b',4,'c',6)
> fn = {'a' 'c'}
>
> for i=1:numel(fn) % This loop seems superfluous
> a(i)=s.(fn{i});
> end
> a % returns [2 6]
>
> The expected solution a = s.(fn) doesn't work. This seems like an
> oversight in MATLAB's implementation of dynamic field references. Or can
> someone explain why this would be a bad idea?

s.(fn{i}) will not usually be the same data type as s.(fn{i+1})

What shape would you expect the result to be if s is a structure array?