From: Nick Evans on 25 Jul 2010 07:31 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 25 Jul 2010 10:26 "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 25 Jul 2010 13:54 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?
|
Pages: 1 Prev: Arithmetic on one column only of an array Next: Radial Bases Function Networks with K means |