Saving figures in a for loop

3 次查看(过去 30 天)
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

采纳的回答

OCDER
OCDER 2018-7-11
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 个评论
dpb
dpb 2018-7-11
Good catch, I thought I'd checked that... :(
OCDER
OCDER 2018-7-11
编辑:OCDER 2018-7-11
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by