Asked by jack gallagher
on 16 Sep 2018

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

Answer by Stephen Cobeldick
on 16 Sep 2018

Edited by Stephen Cobeldick
on 16 Sep 2018

Accepted Answer

"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.

jack gallagher
on 17 Sep 2018

Here it is. I thought that I had already attached it. Sorry about that.

jack gallagher
on 17 Sep 2018

Stephen Cobeldick
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.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.