Filter löschen
Filter löschen

Saving figures in a for loop

3 Ansichten (letzte 30 Tage)
Cameron Power
Cameron Power am 11 Jul. 2018
Bearbeitet: OCDER am 11 Jul. 2018
I am trying to save each plot in a for loop but I omly end up with Day_31.png, the last plot, this is the could I have;
k = 31;
for l = 1:k
Days = ShearEnv_1.Day;
index = Days == l;
ShearA = ShearEnv_1(index,:);
ShearB = figure,
plot(ShearA{:,5}, ShearA{:,12}.*10, 'b-'),
ylim([0 360]),
grid minor;
saveas(ShearB,sprintf('Day_%d.png',k));
end

Akzeptierte Antwort

OCDER
OCDER am 11 Jul. 2018
You're saving 31 graphs to the same file name, 'Day_31.png' due to wrong use of iterator k.
k = 31;
for l = 1:k
Days = ShearEnv_1.Day;
index = Days == l;
ShearA = ShearEnv_1(index,:);
ShearB = figure,
plot(ShearA{:,5}, ShearA{:,12}.*10, 'b-'),
ylim([0 360]),
grid minor;
saveas(ShearB,sprintf('Day_%d.png',l)); <==== not k, which is 31. use l as shown.
end
  2 Kommentare
dpb
dpb am 11 Jul. 2018
Good catch, I thought I'd checked that... :(
OCDER
OCDER am 11 Jul. 2018
Bearbeitet: OCDER am 11 Jul. 2018
I'm surprised the loop even finished, considering this other error that was hiding there ...
plot(ShearA{:, 5}, ShearA{:, 12}.*10, 'b-'); %??
See below for details:
ShearA = num2cell(randi(10, 100, 12)); %Assuming ShearA is a cell
plot(ShearA{:, 5}, ShearA{:, 12}.*10, 'b-'); %This will not work
plot([ShearA{:, 5}], [ShearA{:, 12}]*10, 'b-'); %This will work

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by