Saving figures in a for loop

1 回表示 (過去 30 日間)
Cameron Power
Cameron Power 2018 年 7 月 11 日
編集済み: OCDER 2018 年 7 月 11 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by