title: Iterating through struct fieldnames in MATLAB
body: <p>My question is easily summarized as: <em>"Why does the following not work?"</em></p>
  
  <pre><code>teststruct = struct('a',3,'b',5,'c',9)
  
  fields = fieldnames(teststruct)
  
  for i=1:numel(fields)
    fields(i)
    teststruct.(fields(i))
  end
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <pre><code>ans = 'a'
  
  ??? Argument to dynamic structure reference must evaluate to a valid field name.
  </code></pre>
  
  <p>Especially since <code>teststruct.('a')</code> <em>does</em> work. And <code>fields(i)</code> prints out <code>ans = 'a'</code>.</p>
  
  <p>I can't get my head around it. </p>