Plots are overriding each other, and I do not know why

638 views (last 30 days)
I cannot figure out why a new figure is not opening when running the code. If I run each section by themselves, it works but when I put them together it does not work. I use the function figure but it is not working. With the first section if I use figure with it, it works but suddenly stops when working when I add the second section of the code. When the code is run, it only opens the second figure.
clear % clear all variables from Workspace
clc % clear Comand Window
close all% close all figures
h = 10;
A = pi/6;
t = 1:30;
V1 = 100;
[x1, y1, MaxY1, X_at_MaxY1, T_at_MaxY1] = projectile(t, A, V1, h);
V2 = 200;
[x2, y2, MaxY2, X_at_MaxY2, T_at_MaxY2] = projectile(t, A, V2, h);
V3 = 300;
[x3, y3, MaxY3, X_at_MaxY3, T_at_MaxY3] = projectile(t, A, V3, h);
V4 = 400;
[x4, y4, MaxY4, X_at_MaxY4, T_at_MaxY4] = projectile(t, A, V4, h);
V5 = 500;
[x5, y5, MaxY5, X_at_MaxY5, T_at_MaxY5] = projectile(t, A, V5, h);
figure(2)
plot(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)
title('Vertical Distance vs. Hor') %Give the Graph a title
xlabel('Horizontal Distance (m)') %Lable the X-axis
ylabel('Vertical Distance (m)') %Lable the Y-axis
h = 10;
V = 100;
t = 1:30;
A1 = pi / 6;
[x6, y6, MaxY6, X_at_MaxY6, T_at_MaxY6] = projectile(t, A1, V, h);
A2 = pi / 4;
[x7, y7, MaxY7, X_at_MaxY7, T_at_MaxY7] = projectile(t, A2, V, h);
A3 = pi / 3;
[x8, y8, MaxY8, X_at_MaxY8, T_at_MaxY8] = projectile(t, A3, V, h);
A4 = 7 * pi / 18;
[x9, y9, MaxY9, X_at_MaxY9, T_at_MaxY9] = projectile(t, A4, V, h);
A5 = pi / 2;
[x10, y10, MaxY10, X_at_MaxY10, T_at_MaxY10] = projectile(t, A5, V, h);
figure(2)
plot(x6,y6,x7,y7,x8,y8,x9,y9,x10,y10)
title('Vertical Distance vs. Horizontal nce'); %Give the Graph a title
xlabel('Horizontal Distance (m)'); %Lable the X-axis
ylabel('Vertical Distance (m)'); %Lable the Y-axis
  1 Comment
Lukas Hagmeyer
Lukas Hagmeyer on 12 Nov 2020
This is a very late answer but may helpful for some guys in the future:
I had more or less the same problem with overwritten subplots that I created in a for-loop.
Within the loop, I changed the position of the subplots and this it the problem.
Corrupt the position of the subplot afterwards and they wont disappear.
p = uipanel('Title','Test');
p.Position = [left up width height]; % left up width height
for i = 1 : 10
pax = subplot(10,1,i,'Parent', p); % left up width height
pax.Position(1) = paxLeft;
%pax.Position(2) = height - i * paxHeight; % here the struggle began
pax.Position(3) = paxWidth;
pax.Position(4) = paxHeight;
handles.(['pax' num2str(i)]) = pax;
end
for i = 1: 10
handles.(['pax' num2str(i)]).Position(2) = height - i * paxHeight;
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Sep 2018
Edited: Stephen23 on 16 Sep 2018
"Plots are overriding each other, and I do not know why"
Summary Because that is exactly what you are telling MATLAB to do. The figure documentation explains that syntax as " 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." The number is just a "handle" to the figure, it has no meaning about how many figures there are (or might be), or what sequence they are created in. The number is quite arbitrary.
Solution Either:
  • Remove the number from both figure calls, or
  • Change them so that each call uses a different number, e.g.:
figure(1)
...
figure(2)
Explanation Your code is currently like this:
...
figure(2)
plot(...)
...
figure(2)
plot(...)
According to the documentation, on the first figure call it creates the figure 2 (if it does not already exist) and you plot some data in it. Then on the second figure call you tell MATLAB to look for figure 2, it finds it (because you just created it a few lines earlier), and so it uses this figure to plot in (and plot by default replaces any existing data in the axes). So your code always refers to the same figure.
"When the code is run, it only opens the second figure."
You only request one figure in your code, and so that is what MATLAB gives you. There is no second figure.
  9 Comments
Stephen23
Stephen23 on 17 Sep 2018
Edited: Stephen23 on 17 Sep 2018
"I just realized that I did have close in the function."
Putting clc, clear, and close at the start of every piece of code is an ugly anti-pattern (proof: the time you wasted tracking down this bug), and should be avoided (not matter how much beginners love doing it).
Question: what does projectile have to do with any graphical operation?
Answer: Absolutely nothing. So why put close all in it? Would you also expect calling sin(0) or sqrt(9) to close all of your figures?
Do not put clear, clc, close everywhere (in well-written code these will be needed only rarely).

Sign in to comment.

More Answers (2)

Giovanni Bambini
Giovanni Bambini on 15 Nov 2023
Edited: Giovanni Bambini on 15 Nov 2023
I have a similar problem.
I'm working on Linux.
The code is too convoluted to share, but, I have several blocks of:
fig1 = figure('Name', 'a name');
movegui(fig, 'northeast');
%plot something
fig2 = ...
%etc.
When I execute the code, figures override each other messing with positions, figure's names, etc.
If I stop the code and move with the F10 key, everything WORKS PERFECTLY. So practically debug is different from execution.
Do you have any idea how to solve it, or if something I'm doing is messing up with the code?
E.

Sven
Sven on 30 Jan 2024
I was struggeling with the same issue that consequitive "area" plot commands would overrite the previous "area" plot command.
I found that the setting "NextPlot" of the uiaxes type controls this behaviour.
Solution: Set the property "NextPlot" of an uiaxes handle to "add"
app.handle_to_my_axis.NextPlot = "add";
Available are "add", "replace", "replacechildren", "replaceall"

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!