필터 지우기
필터 지우기

Saving figures in a for loop

조회 수: 3 (최근 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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by