title: How do I access structure fields dynamically?
body: <p>I have a structure with many fields which are vectors of different lengths. I would like to access the fields within a loop, in order. I tried getfield as follows but MATLAB doesn't like that. How can I do this?  </p>
  
  <pre><code>S = struct('A', [1 2], 'B',[3 4 5]);
  SNames = fieldnames(S);
  for loopIndex = 1:2
    field = getfield(S, SNames(loopIndex));
    %do stuff w/ field
  end
  ??? Index exceeds matrix dimensions
  </code></pre>
  
  <p>I'm using structures in the first place because an array would have trouble with the different field lengths. Is there a better alternative to that?</p>