Re: "global" bug 2.1.39?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: "global" bug 2.1.39?

John W. Eaton-6
On 20-Nov-2002, Paul Kienzle <[hidden email]> wrote:

| Shouldn't that be:
|
|    global c d e
|    if ~exist('c','var'), c=3; end
|    if ~exist('e','var'), e=5; end
|
| otherwise it will overwrite the previously initialized values if there
| are any.

Oops, my example was wrong, but the above won't work in Matlab either,
because global variables are initialized to [].  So you need some
other way to tell that they have been initialized.  For example,

  function test_global_init ()
    persistent my_globals_are_initialized
    global a b c
    if (isempty (my_globals_are_initialized))
      a = 1
      b = 2
      c = 3
      my_globals_are_initialized = true;
    end
    a, b, c

Also, in Octave, you would need to set

  initialize_global_variables = 1;
  default_global_variable_value = [];

somewhwere in your code.

Also, I just noticed that there is an incompatibility in the isglobal
function (perhaps this is well-known, but I don't remember it).  In
Octave, we expect a string naming the variable and Matlab appears to
expect an expression:

  global A; A = 1;
  isglobal ('A')  ==>  true in Octave, false in Matlab
  isglobal (A)    ==>  false in Octave, true in Matlab
 
This doesn't make much sense to me.  In the second use of isglobal
above, I think of the expression A as having a value that is passed to
the isglobal function, but that value is not tagged as global.  You
wouldn't ask whether a value is global, only whether a variable name
is global.  Hmm.  Duplicating this behavior would mean either making
isglobal some kind of special syntax, or making some major changes in
Octave that would allow unevaluated argument lists to be passed to a
function (as far as I know, Matlab doesn't have that, and I'm not
really in favor of this change for Octave at this point).

Let's move this part of the discussion to the octave-maintainers
mailing list.

Thanks,

jwe