title: Convert structure array to matrix
body: <p>I am trying to convert a structure array to a matrix. Each field of the structure stores a vector that can reach up to 520000 rows. There can be up to 20 fields in a single structure array, but number of rows is the same across the fields.</p>
  
  <p>As a down scaled example consider the structure <code>s</code>, where each field is an integer:</p>
  
  <pre><code>s=struct('a',1,'b',2);
  s(2)=struct('a',3,'b',4);
  s=s';
  </code></pre>
  
  <p>In the desired output, each field will correspond to a column. <code>a</code> values will be in the first column, while <code>b</code> values will be in the second:</p>
  
  <pre><code>desiredOutput = [1 2; 3 4];
  </code></pre>
  
  <p>I have approached this in an indirect way:</p>
  
  <pre><code>cell2mat(struct2cell(s))'
  </code></pre>
  
  <p>However, this involves two transformations which i find unnecessary due to the well behaved nature of my structure. </p>
  
  <p>I have also approached this using a for loop:</p>
  
  <pre><code>fields = fieldnames(s);
  nrows = size(s,1);
  ncols = numel(fields);
  desiredOutput = nan(nrows,ncols);
  for jj=1:ncols
      desiredOutput(:,jj) = [s.(fields{fields(jj)})]';
  end
  </code></pre>
  
  <p>I hoped to find a <code>struct2mat</code> function but it does not exist. Is there a simpler way to accomplish this task that I am not aware of?</p>