Problem using hold all

2 views (last 30 days)
Elisa Pacheco
Elisa Pacheco on 4 Oct 2017
Commented: Elisa Pacheco on 4 Oct 2017
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?

Accepted Answer

Jan
Jan on 4 Oct 2017
Edited: Jan on 4 Oct 2017
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 Comment
Elisa Pacheco
Elisa Pacheco on 4 Oct 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!