How to save a figure through each run of a for loop?
    4 views (last 30 days)
  
       Show older comments
    
I created a for loop for a series of figures, and am trying to save each one as the for loop is running (saved as 'Delta_roseplot_forloop_Trial1.jpg', '...Trial2.jpg', '...Trial3.jpg', and so forth). 
Here is what I have:
for m= 1:6
    load('BR_PD10_SRTT_pwr_M1.mat')
    itc_delta=itc_trial{m};
    itc_delta=itc_delta(2,:,:);
    itc_delta=squeeze(itc_delta);
    itc_delta_keypress=itc_delta(398,:); 
    f1=figure;
    rose(itc_delta_keypress); 
    for k=1:6
        h=f1;
        rose(itc_delta_keypress);
        saveas(h,sprintf('Delta_roseplot_forloop_Trial%d.jpg',k)); 
    end
end 
But while all the figures come out right, the .jpgs are all saved looking exactly the same. I think I'm supposed to put 'hold on' somewhere, but I can't figure out where. 
Would appreciate any help!
1 Comment
  Walter Roberson
      
      
 on 31 Jan 2019
				The only thing you change inside your for k loop is the filename to write to, so unless rose() uses random numbers internally, all of the rose plots are going to be the same.
Note: the filename you construct does not depend upon m, so you are not saving one file for each m, k combination.
Answers (1)
  KSSV
      
      
 on 1 Feb 2019
        load('BR_PD10_SRTT_pwr_M1.mat')
for m= 1:6    
    itc_delta=itc_trial{m};
    itc_delta=itc_delta(2,:,:);
    itc_delta=squeeze(itc_delta);
    itc_delta_keypress=itc_delta(398,:); 
    figure
    rose(itc_delta_keypress); 
    for k=1:6        
        rose(itc_delta_keypress);
        saveas(gcf,strcat('Delta_roseplot_forloop_Trial',num2str(k),'.jpg')) ;        
    end
end 
0 Comments
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

