Problem using hold all

2 vues (au cours des 30 derniers jours)
Elisa Pacheco
Elisa Pacheco le 4 Oct 2017
Commenté : Elisa Pacheco le 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?

Réponse acceptée

Jan
Jan le 4 Oct 2017
Modifié(e) : Jan le 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 commentaire
Elisa Pacheco
Elisa Pacheco le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by