How to save a figure through each run of a for loop?

4 visualizzazioni (ultimi 30 giorni)
Maria Y
Maria Y il 31 Gen 2019
Risposto: KSSV il 1 Feb 2019
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 Commento
Walter Roberson
Walter Roberson il 31 Gen 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.

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by