Problem using hold all

조회 수: 2 (최근 30일)
Elisa Pacheco
Elisa Pacheco 2017년 10월 4일
댓글: Elisa Pacheco 2017년 10월 4일
Hi,
I have a while loop in matlab where I am updating some variables and vectors (I have ommited that part from the code because it isn't relevant for this issue). I would like to have a plot with the the values of these variables and vectors between iterations. I have the following code:
j=0;
while j<=Niter
(...)
figure(1); subplot(1,2,1); plot(tspan,H_v_c); hold all; subplot(1,2,2); stem(j,J_new); hold all;
figure(2);
subplot(1,2,1); plot(tspan,xout(:,1)+xout(:,2),'r',tspan,xout(:,3),'g',tspan,xout(:,4),'b',tspan,xout(:,5),'c');
subplot(1,2,2); plot(tspan,v_chemo); hold all;
j=j+1;
end
However some of the values of J_new from figure(1) subplot(1,2,2) appear in figure(2) subplot(1,2,2). Is there something that I can do to avoid this?

채택된 답변

Jan
Jan 2017년 10월 4일
편집: Jan 2017년 10월 4일
It is safer and in my opinion easier to specify the parents explicitly:
Fig1 = figure;
Axes1_1 = subplot(1,2,1, 'Parent', Fig1, 'NextPlot', 'add'); % Equivalent to: hold on
Axes1_2 = subplot(1,2,2, 'Parent', Fig1, 'NextPlot', 'add');
Fig2 = figure(2);
Axes2_1 = subplot(1,2,1, 'Parent', Fig2, 'NextPlot', 'add');
Axes2_2 = subplot(1,2,2, 'Parent', Fig2, 'NextPlot', 'add');
j=0;
while j<=Niter % A FOR loop might be nicer
(...)
plot(tspan,H_v_c, 'Parent', Axes1_1);
stem(j,J_new, 'Parent', Axes1_2);
plot(tspan, xout(:,1)+xout(:,2), 'r', 'Parent', Axes2_1);
plot(tspan, xout(:,3), 'g', 'Parent', Axes2_1);
plot(tspan, xout(:,4), 'b', 'Parent', Axes2_1);
plot(tspan, xout(:,5), 'c', 'Parent', Axes2_1);
plot(tspan, v_chemo, 'Parent', Axes2_2);
j=j+1;
end
Note that in modern Matlab versions (HG2, 2014b) this is safe also to define the parent:
plot(Axes2_2, ...)
This should be faster also, because it avoids the switching of the active figures and axes.
  댓글 수: 1
Elisa Pacheco
Elisa Pacheco 2017년 10월 4일
Thank you so much, Jan Simon. I'll try it. Concerning the for loop, I can't use it because in my code (this was a simpler version) I also evaluate some booleans.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by