Why does figure(2) keeps overwriting the previous figure?

I try to open figure(2) in a different window but it keeps overwriting the previous figure.
I am currently working with MATLAB R2020a.
I tried something very simple as such:
figure(1), plot([0:10],[0:10])
figure(2), plot([0:10],[10:-1:0])
figure(3), plot([0:10],rand(1,11))
figure(1) and (3) always open in a separate window but figure(2) keeps overwriting figure(1).
See attached figures
Does anyone know how to fix this?
Thank you

7 Comments

The three figures you attached seem to match the three plot commands you provided, can you clarify what you were expecting?
Just to show your code in R2021a behaves as expected:
figure(1), plot([0:10],[0:10])
figure(2), plot([0:10],[10:-1:0])
figure(3), plot([0:10],rand(1,11))
Thank you for your answers.
Yes, it shows the proper plots but figure(2) always overwrites figure(1).
Instead of having a different window just like what I get for figure(3), figure(2) always open in figure(1) instead of opening in a separate figure(2) window.
I can't have both figure(1) and figure(2) open at the same time...
Do you know how to fix this issue?
Thank you
Can you confirm this is the exact code you're using?
And can you show the results of this line of code?
which figure -all
built-in (/MATLAB/toolbox/matlab/graphics/figure)
Do you mean this is what appears when you run the code you included? That would be very surprising.
Or when you open the .fig file? That would be unsurprising, the figure window is not saved in the .fig file...
I am just running the commands in my MATLAB Command Window and it keeps doing the same thing.
There is no problem for figure(3), (4), (5) or anything over.
The problem is only for figure(2).
I have attached a screenshot of the commands I'm running in my MATLAB Command Window.
Here is the code I am running and the command Risk asked for.
>> figure(1), plot([0:10],[0:10])
>> figure(2), plot([0:10],[10:-1:0])
>> figure(3), plot([0:10],rand(1,11))
>> which figure -all
built-in (C:\Program Files\MATLAB\R2020a\toolbox\matlab\graphics\figure)
Thank you
I know I'm late to the party, but if you run clf at the beginning of your script, this happens for whatever reason

Sign in to comment.

Answers (1)

use save command to see whether it is working
surf(peaks)
savefig('MySavedPlot.fig')
close(gcf)
I was running your three lines in matlab 2020b and it shows seperate figures with correct labels.
check your other parts of your code to see whether something shadows your plot

2 Comments

Thank you Sina Rangriz for your answer.
I just wanted to display the figures without saving them to compare them on my screen when I run a code.
I can save them just fine an them open the figures in different windows but my problem is that I want to see my different plots in different windows when I run my code.
Thank you
This problem is kinda weird because on my computer your attached codes working well....
figure(n) finds a figure in which the Number property is equal to n, and makes it the current figure. If no figure exists with that property value, MATLAB® creates a new figure and sets its Number property to n.
try with empty figure....like:
figure, plot([0:10],[0:10])
figure, plot([0:10],[10:-1:0])
figure, plot([0:10],rand(1,11))

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Asked:

on 10 Aug 2021

Commented:

on 22 Apr 2023

Community Treasure Hunt

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

Start Hunting!