Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
All the output is correct, accept I get this global variable error message, please help correct my code...

Subject: All the output is correct, accept I get this global variable error message, please help correct my code...

From: Billy Johnson

Date: 25 Apr, 2009 17:39:01

Message: 1 of 4

There are a total of 3 short programs I wrote, which plots a graph of some projectile motion equations. The graph comes out nice and as expected but I get this error message about global variables.
%Program 1
function f_plot(x,y)
global x0 y0 v0 T t g x y
figure,plot(x,y,'d-','MarkerFaceColor','b'),title('Projectile Motion for Zero Drag'),xlabel('x(m)'),ylabel('y(m)');
end

%Program 2
function f_zerodrag(x0,y0,v0,T,t)
global x0 y0 v0 T t g x y
x=x0+v0*cos(T)*t;
y=y0+v0*sin(T)*t-.5*g*t.^2;
end

%Program 3 - Calling Program
global x0 y0 v0 T t g x y
g=9.81;
t=[0:1:14];
x0=0;
y0=0;
v0=70;
T=70*pi/180;
f_zerodrag(x0,y0,v0,T,t);
f_plot(x,y);

I get this error message (but a correct graph):
>> run fcall_zerodrag
Warning: The value of local variables may have been changed to match the
         globals. Future versions of MATLAB will require that you declare
         a variable to be global before you use that variable.
> In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\Week 13\f_zerodrag.m at line 2
  In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\Week 13\fcall_zerodrag.m at line 8
  In C:\MATLAB6p5\toolbox\matlab\lang\run.m at line 73

Subject: All the output is correct, accept I get this global variable

From: ImageAnalyst

Date: 25 Apr, 2009 18:04:32

Message: 2 of 4

On Apr 25, 1:39=A0pm, "Billy Johnson" <blahblah15...@hotmail.com> wrote:
> There are a total of 3 short programs I wrote, which plots a graph of som=
e projectile motion equations. =A0The graph comes out nice and as expected =
but I get this error message about global variables.
> %Program 1
> function f_plot(x,y)
> global x0 y0 v0 T t g x y
> figure,plot(x,y,'d-','MarkerFaceColor','b'),title('Projectile Motion for =
Zero Drag'),xlabel('x(m)'),ylabel('y(m)');
> end
>
> %Program 2
> function f_zerodrag(x0,y0,v0,T,t)
> global x0 y0 v0 T t g x y
> x=3Dx0+v0*cos(T)*t;
> y=3Dy0+v0*sin(T)*t-.5*g*t.^2;
> end
>
> %Program 3 - Calling Program
> global x0 y0 v0 T t g x y
> g=3D9.81;
> t=3D[0:1:14];
> x0=3D0;
> y0=3D0;
> v0=3D70;
> T=3D70*pi/180;
> f_zerodrag(x0,y0,v0,T,t);
> f_plot(x,y);
>
> I get this error message (but a correct graph):>> run fcall_zerodrag
>
> Warning: The value of local variables may have been changed to match the
> =A0 =A0 =A0 =A0 =A0globals. =A0Future versions of MATLAB will require tha=
t you declare
> =A0 =A0 =A0 =A0 =A0a variable to be global before you use that variable.>=
 In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\Week =
13\f_zerodrag.m at line 2
>
> =A0 In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\=
Week 13\fcall_zerodrag.m at line 8
> =A0 In C:\MATLAB6p5\toolbox\matlab\lang\run.m at line 73

---------------------------------------------------------------------------=
------------------
The problem is that if you're going to declare the variable as global,
then there is no need to pass it via the argument list. That will
possibly cause some kind of confusion. Either pass them in the
argument list, or declare them as global, but not both.

Subject: All the output is correct, accept I get this global variable

From: Billy Johnson

Date: 25 Apr, 2009 18:44:02

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7a1a8919-0bf0-44f1-9691-dc5fd974bd8d@v23g2000pro.googlegroups.com>...
> On Apr 25, 1:39=A0pm, "Billy Johnson" <blahblah15...@hotmail.com> wrote:
> > There are a total of 3 short programs I wrote, which plots a graph of som=
> e projectile motion equations. =A0The graph comes out nice and as expected =
> but I get this error message about global variables.
> > %Program 1
> > function f_plot(x,y)
> > global x0 y0 v0 T t g x y
> > figure,plot(x,y,'d-','MarkerFaceColor','b'),title('Projectile Motion for =
> Zero Drag'),xlabel('x(m)'),ylabel('y(m)');
> > end
> >
> > %Program 2
> > function f_zerodrag(x0,y0,v0,T,t)
> > global x0 y0 v0 T t g x y
> > x=3Dx0+v0*cos(T)*t;
> > y=3Dy0+v0*sin(T)*t-.5*g*t.^2;
> > end
> >
> > %Program 3 - Calling Program
> > global x0 y0 v0 T t g x y
> > g=3D9.81;
> > t=3D[0:1:14];
> > x0=3D0;
> > y0=3D0;
> > v0=3D70;
> > T=3D70*pi/180;
> > f_zerodrag(x0,y0,v0,T,t);
> > f_plot(x,y);
> >
> > I get this error message (but a correct graph):>> run fcall_zerodrag
> >
> > Warning: The value of local variables may have been changed to match the
> > =A0 =A0 =A0 =A0 =A0globals. =A0Future versions of MATLAB will require tha=
> t you declare
> > =A0 =A0 =A0 =A0 =A0a variable to be global before you use that variable.>=
> In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\Week =
> 13\f_zerodrag.m at line 2
> >
> > =A0 In C:\Documents and Settings\Christopher\Desktop\MAE 2100 2\MAE 2100\=
> Week 13\fcall_zerodrag.m at line 8
> > =A0 In C:\MATLAB6p5\toolbox\matlab\lang\run.m at line 73
>
> ---------------------------------------------------------------------------=
> ------------------
> The problem is that if you're going to declare the variable as global,
> then there is no need to pass it via the argument list. That will
> possibly cause some kind of confusion. Either pass them in the
> argument list, or declare them as global, but not both.

I looked at the assignment and I noticed I didn't have to use globals, but I had to use three programs. Basically what I did is defined all of the variables within program 2, and deleted all global statements I made in all three programs. Correct me if this is bad to define variables within the function? I am happy at least I have no error messages now.

Subject: All the output is correct, accept I get this global variable

From: ImageAnalyst

Date: 25 Apr, 2009 21:36:00

Message: 4 of 4

On Apr 25, 2:44=A0pm, "Billy Johnson" <blahblah15...@hotmail.com> wrote:
[snip]
> I looked at the assignment and I noticed I didn't have to use globals, bu=
t I had to use three programs. =A0Basically what I did is defined all of th=
e variables within program 2, and deleted all global statements I made in a=
ll three programs. =A0Correct me if this is bad to define variables within =
the function? =A0I am happy at least I have no error messages now.- Hide qu=
oted text -
>
----------------------------------------------------------
Creating variables in your functions and then passing ONLY the
variables that are needed into the subfunctions via the argument list
is probably the preferred way of programming. Sounds like you are now
doing that. Good job.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us