Borrar filtros
Borrar filtros

Saving figures in a for loop

3 visualizaciones (últimos 30 días)
Cameron Power
Cameron Power el 11 de Jul. de 2018
Editada: OCDER el 11 de Jul. de 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

Respuesta aceptada

OCDER
OCDER el 11 de Jul. de 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 comentarios
dpb
dpb el 11 de Jul. de 2018
Good catch, I thought I'd checked that... :(
OCDER
OCDER el 11 de Jul. de 2018
Editada: OCDER el 11 de Jul. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by