setting global variables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

setting global variables

Fritz Sonnichsen
I have been using the "global" directive to set up some (around 50)
global variables in a script. However in order to access them from
functions in the same script, I need to define them as global again or
they are not defined in the function.
    Seems a bit redundant to me--is there some way to define variables
share by all functions just once?

Thanks
Fritz



Reply | Threaded
Open this post in threaded view
|

RE: setting global variables

Tony Richardson
> To: Help GNU Octave <[hidden email]>
> Subject: setting global variables
>
> I have been using the "global" directive to set up some (around 50) global variables in a script. However in order to access them from functions in the > same script, I need to define them as global again or they are not defined in the function.
>     Seems a bit redundant to me--is there some way to define variables share by all functions just once?
>
> Thanks
> Fritz

You can achieve a similar effect (although perhaps not quite as convenient) by using a persistent struct inside a data retrieval function.  I use a function named simState() for this purpose.

To define and set the value of variable from within one function or script:
   simState("TMAX", 1200.)
Then to retrieve the value from within any other function:
   TMAX = simState("TMAX")

I have included simState() below.    I use it for a purpose like what you describe.  I want to access a global variable from multiple functions.  Note that I retrieve a copy of the "global" value.  If I want to change the global value, I need to use simState("TMAX", newvalue).

Tony Richardson

function v = simState(key, val)
  persistent state;
  if (nargin == 0)  
    % Return the entire struct
    v = state;
  elseif (nargin == 1)
    % Return a particular field from the struct
    v = state.(key);
  elseif (nargin == 2)
    % Set a key value (key must be a string)
    state.(key) = val;
    v = val;
  end
end



Reply | Threaded
Open this post in threaded view
|

Re: setting global variables

Ian McCallion
In reply to this post by Fritz Sonnichsen
On Fri, 26 Oct 2018, 18:41 Fritz Sonnichsen, <[hidden email]> wrote:
I have been using the "global" directive to set up some (around 50)
global variables in a script. However in order to access them from
functions in the same script, I need to define them as global again or
they are not defined in the function.
    Seems a bit redundant to me--is there some way to define variables
share by all functions just once?

The language is defined that way for good practical reasons so there is no way to do exactly what you want. My way round the problem is to define a single global structure:

  global ggg

All my functions that need any global data include this statement. Then all my global variables are fields of this structure:

   ggg.windowsize=50;
   ggg.hopsize = 10;

Doing this is actually quite conveniently self-documenting and it is useful during debugging to be able to display all the global values simply by typing:

    ggg

at the console.

The other idea I tried and eventually rejected, which only works if you are using globals as a way of sharing constants, is to define a trivial function in its own file to embody the value:

    function [ret] = windowsize()
        ret = 50;
    endfunction

In any function you can then use windowsize in an expression just as though it were a variable, but obviously using it on the left of an assignment statement will not have the desired effect.

Hope this helps.

Cheers... Ian


Reply | Threaded
Open this post in threaded view
|

Re: setting global variables

Andreas Weber-6
In reply to this post by Fritz Sonnichsen
Am 26.10.18 um 19:40 schrieb Fritz Sonnichsen:
> I have been using the "global" directive to set up some (around 50)
> global variables in a script. However in order to access them from

In my opinion global variables aren't needed and shouldn't be used. When
I've started using GNU Octave >15years ago I thought I have to use them
but after I got more familiar with the language and I discovered
anonymous functions and guidata, appdata (which is basically global) I
never had to use globals again.

-- Andy




Reply | Threaded
Open this post in threaded view
|

Re: setting global variables

Ian McCallion
On Sat, 27 Oct 2018, 09:31 Andreas Weber, <[hidden email]> wrote:
Am 26.10.18 um 19:40 schrieb Fritz Sonnichsen:
> I have been using the "global" directive to set up some (around 50)
> global variables in a script. However in order to access them from

In my opinion global variables aren't needed and shouldn't be used. When
I've started using GNU Octave >15years ago I thought I have to use them
but after I got more familiar with the language and I discovered
anonymous functions and guidata, appdata (which is basically global) I
never had to use globals again.


I understand the general objections to globals but alternatives doing the same thing should help in terms of code clarity and/or compactness and/or performance and I can't see at the moment how your solution is better. What am I missing?

Cheers... Ian