Re: global variables



On 2006-09-15 14:41 Robin Northcott said the following:
I have a script that uses global variables to pass information
between functions. I noticed some problems so I started looking at
the debugger and noticed that the first time these variables are
declared (global .....)they are created with values. I think this is
causing my problems, but I don't know why it is happening or how to
stop it.

This is the first reference to these variables, they do not already
exist in the work space. I don't want to use them as function
arguments, it is much more conveniant for me to have them as global.

Also can anyone explain what the difference is between effective and
persistant variables? should I be using persistant variables.

The most frustratibng thing is that this part of the script only
started giving me problems today, I have been working on this for
over a week and don't understand what I did that created this problem.

Problems with global variables can be very tricky to debug. Read the document here:
http://www.mathworks.com/support/solutions/data/1-15LUQ.html?solution=1-15LUQ

I believe that what happens to you is described in the section about Creating global variables. (2. If no variable .... etc.)

I would recommend you to create one single global structure and collect all the variables in that structure, something like this:

global globvars

globvars.alpha=asin(3);
globvars.force=10;
globvars.mass=[1 0 0;0 7 0;0 0 5];
globvars.name='Robin'

This makes it a lot easier to have an overview of the global variables, and you won't risk name clashes with local variables (often the cause of trouble).

HTH

/PB

--
"Never attribute to malice that which can be adequately explained by stupidity."
.



Relevant Pages

  • global variables
    ... the debugger and noticed that the first time these variables are ... should I be using persistant variables. ... The most frustratibng thing is that this part of the script only ...
    (comp.soft-sys.matlab)
  • global variables
    ... the debugger and noticed that the first time these variables are ... should I be using persistant variables. ... The most frustratibng thing is that this part of the script only ...
    (comp.soft-sys.matlab)
  • Re: Extremely slow debugging performance in VS2003 with interop
    ... in the debugger now executes CreateInstance in 12 secs instead of 45. ... However, the second time I run the app, I'm back ... to 45 seconds every first time I run the app in the debugger. ... Here is the 12 second output: ...
    (microsoft.public.vsnet.debugging)
  • Re: Extremely slow debugging performance in VS2003 with interop
    ... > rename the file to symsrv.dll.hide, start VS and the initial run of the app ... > in the debugger now executes CreateInstance in 12 secs instead of 45. ... > to 45 seconds every first time I run the app in the debugger. ... > Here is the 12 second output: ...
    (microsoft.public.vsnet.debugging)
  • Re: Accessing Session Variable
    ... when I run the script for the first time. ... If it still gives a problem, your PHP install ... If you are not using a debugger, ... That's free software. ...
    (php.general)