Figure(2) is getting replaced by figure(1) and figure(3) by figure(2) in a for loop and so on.

16 vues (au cours des 30 derniers jours)
In general, if there is a for loop running from i=1...L for a plot like below
L=5;
for i=1:L
.
.
figure(i)
plot()
.
.
end
There are 5 different windows that open with titles figure(1), figure(2) ... figure(5) that open and plot different stuff according to the parameters given in the plot.
However in one code of mine shown below, this is not happening
L=5;
figure(1);
plot();
for l=2:L
[g,G]=my_own_function1();
figure(l);
w=[];
subplot(2,1,1);
plot(w,G);
figure(l);
[g2,G2] = my_own_function2();
w=[];
subplot(2,1,2);
plot(w,G2);
end
In this code, figure(1) is getting replaced by figure(2) and figure(2) is getting replaced by figure(3) and so on.. Can somebody tell me why different windows are not opening for this code?

Réponses (1)

Guillaume
Guillaume le 10 Avr 2019
As explained in the documentation of figure, when you specify a number in the figure call, "MATLAB searches for an existing figure in which the Number property is equal to n", so possibly your code has changed the Number property of the figures.
In any case, the best way to ensure you're targeting the correct figure is to keep around the figure handles of the figures you've created and explicitly pass these handles to all graphics function. You're then guaranteed that your plot appears on the correct figure, even if some other code or the user happens to change the correct figure.
hfig = figure;
%...
plot(axes(hfig), ...); %guaranteed to plot in hfig even if its number has changed and whether or not it's the current figure
  5 commentaires
Anant Bhushan Nagabhushana
The Full Code:
%% Common Initialzations
Fs = 61.44;
TrBand = 0.4;
Rs = 45;
Rp = 1;
%% Plotting H -- The desired plot
FbH = [10 15]; % The lower and higher cutoff frequencies
[h,H]=BPFbaseband_func(FbH,Fs,TrBand,Rs,Rp);
figure(1);
w = (-Fs/2:Fs/1024:(Fs/2)-(Fs/1024));
plot(w,20*log10(abs(H)));
%%
L=5;
for l=2:L
disp('l just inside for loop='); disp(l);
FbG = [10*l 15*l];
w = (-Fs/2:Fs/1024:(Fs/2)-(Fs/1024));
[g,G]=BPFbaseband_func(FbG,Fs,TrBand,Rs,Rp);
[g2,G2] = h_interpolated(g,l); %My own function.
disp('l just before plotting='); disp(l);
%figure(l);
hfig=figure;
subplot(2,1,1)
plot(axes(hfig),w,20*log10(abs(G)));
subplot(2,1,2)
plot(axes(hfig),w,20*log10(abs(G2)));
end
Anant Bhushan Nagabhushana
What is happening is that figure(1), figure(2), figure(3).. figure(5) are getting plotted but on the same window...

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots 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