title: MATLAB: Determine total length/size of a structure array with fields as structure arrays
body: <p>I have a structure array containing fields as structure arrays of varying length. For example:</p>
  
  <p>'s' is a structure
  'data' is a field in 's', and also a structure array itself
  and</p>
  
  <pre><code>length(s(n).data) ~= length(s(m).data)
  </code></pre>
  
  <p>I want to preallocate an array that takes a time stamp from every field s.data.timestamp.
  Is there a way to do this without using a for loop twice? This is what I have so far:</p>
  
  <pre><code>% find the total length
  count=0;
  for x=1:length(s)
    count=count+length(s(x).data);
  end
  % preallocate timestamp array
  timestamp=zeros(1,count);
  % populate timestamp array
  index=1;
  for x=1:length(s)
     for y=1:length(s(x).data)
        timestamp(index)=s(x).data(y).timestamp;
        index=index+1;
     end
  end
  </code></pre>
  
  <p>I thought about just overestimating the length that I would need based on the length of 's' and an average length of 'data', but the actual length of each 'data' field/substructure varies widely. Would I be better off just overestimating the heck out of it and trimming the resulting array afterward? Zero timestamps are impossible with the data set I'm working with, so that shouldn't be a problem.</p>