Re: global variables
- From: PB <pbodin@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 15 Sep 2006 14:10:03 GMT
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."
.
- Follow-Ups:
- Re: global variables
- From: Robinm Northcott
- Re: global variables
- References:
- global variables
- From: Robin Northcott
- global variables
- Prev by Date: Re: Naming lines in Plots
- Next by Date: a random matrix of zeros and ones with exactly N ones
- Previous by thread: global variables
- Next by thread: Re: global variables
- Index(es):
Relevant Pages
|