Problem using hold all
조회 수: 2 (최근 30일)
이전 댓글 표시
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?

댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!