From bug-octave-request at bevo dot che dot wisc dot edu Wed Nov 20 10:48:22 2002 Subject: Re: "global" bug 2.1.39? From: "John W. Eaton" To: Paul Kienzle Cc: Randy Gober , bug-octave@bevo.che.wisc.edu, octave-maintainers mailing list Date: Wed, 20 Nov 2002 10:47:55 -0600 On 20-Nov-2002, Paul Kienzle 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 ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------