title: Plotting and saving a plot in a loop after the entire loop is done
body: <p>I need to keep updating a plot within a loop because I am doing a linear regression for each segment in space. I can do that just fine and display a correct plot. But however I don't seem to be able to save the final plot to file. My code looks something like:</p>
  
  <pre><code>for i = 1:slabs
  
      %.....SOME LOOPED RESULTS HERE, SHORTENED FOR BREVITY.....
  
      p = polyfit(collectCoord, collectTemp, 1);
      t2 = floor(min(collectCoord)) : 0.1 : ceil(max(collectCoord));
      y2 = polyval(p,t2);
  
      h = plot(collectCoord, collectTemp, 'o', t2, y2);
      xlabel('X-Coordinate')
      ylabel('Temperature')
      axis([-8 8 50 800])
      hold on     
  end
  
  filename = [folder 'Plot' num2str(stepCount) '.jpg'];
  saveas(h, filename);
  </code></pre>
  
  <p>What exactly is it that I am doing wrong here, or is there a much better way of saving the plot?</p>