[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Checking existance of global variable in function?
From: |
John W. Eaton |
Subject: |
Re: Checking existance of global variable in function? |
Date: |
Wed, 19 Jul 2006 15:05:41 -0400 |
On 16-Jul-2006, Etienne Grossmann wrote:
|
| Btw, this is more a address@hidden mail, iigic.
|
| On Sun, Jul 16, 2006 at 10:16:24AM -0400, Etienne Grossmann wrote:
| #
| # Hi Volker,
| #
| # what about testing whether graphdir is a string, as in:
| #
| # function [out] = plotinit (eps, file, gnuplotfile)
| # global graphdir
| # if (exist('graphdir') ~= 0)
| # if ~ischar(graphdir), graphdir = ''; end
| # graphdir=[graphdir '']
If a variable is declared global, it will exist, so the
if (exist('graphdir') ~= 0)
line is not really needed.
Globals are initialized to [] by default for compatibility with
Matlab. Octave also allows initialization in the global statement:
global graphdir = "/some/default/dir";
but that does not work with Matlab. If you need your code to run in
both systems, then you are stuck with the initial value being []. If
you need to know whether a global variable has been initialized and
a valid value for the global variable is [] and this is not the
initial value you desire, then you need a auxiliary variable to keep
track of whether the global has been initialized. For example,
something like
global foo;
persistent initialized;
if (isempty (intialized))
foo = some_function ();
initialized = true;
end
should work.
jwe