Problem using hold all

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