title: How do i save my output array from a loop?
body: <p>I have a loop which outputs variable <code>A</code> as an <code>mx1 matrix (where: m&gt;1)</code> for each iteration. At the end of the entire loop, <code>A</code> will end up as an <code>mxn matrix</code>. Although the output is returned as an <code>mxn</code> matrix, it however keeps overwriting the results. I have tried so many stuffs (including the ones that have worked for my previous codes) but none seem to work. Nevertheless, i feel the code below should work but i'm not sure where the error is coming from.</p>
  
  <pre><code>x_A = NaN(28, 3); % 28=length of A and 3=length of kk
  Z = [1,2,4,7];
  
  for kk = 1: numel(Z)
     [A,B,C] = fsave_output(Z, kk)
     x_A     = [x_A(:,kk) A(:,kk)];
  
     % repeat the line above for B and C
  
  end
  </code></pre>
  
  <p>When i run this, i get the error message: <strong>"Attempted to access x_A(:,3); index out of bounds because size(x_norm)=[28,2]."</strong>. However, the outputs in x_A(:,1) and X_A(:,2) are correct, only that x_A(:,3) doesn't show.</p>
  
  <p>Any ideas/suggestions/help on what i should do here? Many thanks in advance!.</p>