Iterating through struct fieldnames in MATLAB.

Posted by Noio on Stack Overflow See other posts from Stack Overflow or by Noio
Published on 2010-05-10T15:32:29Z Indexed on 2010/05/10 15:34 UTC
Read the original article Hit count: 179

Filed under:
|

My question is easily summarized as: "Why does the following not work?"

teststruct = struct('a',3,'b',5,'c',9)

fields = fieldnames(teststruct)

for i=1:numel(fns)
  fns(i)
  teststruct.(fns(i))
end

output:

ans = 'a'

??? Argument to dynamic structure reference must evaluate to a valid field name.

Especially since teststruct.('a') does work. And fns(i) prints out ans = 'a'.

I can't get my head around it.

© Stack Overflow or respective owner

Related posts about matlab

Related posts about struct