Figure(2) is getting replaced by figure(1) and figure(3) by figure(2) in a for loop and so on.
16 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Guillaume
on 10 Apr 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 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!