Discussion:
setting global variables
Fritz Sonnichsen
2018-10-26 17:40:34 UTC
Permalink
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 function
Richardson, Anthony
2018-10-26 18:05:59 UTC
Permalink
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
Ian McCallion
2018-10-27 05:25:07 UTC
Permalink
Post by 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?
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
Andreas Weber
2018-10-27 08:29:56 UTC
Permalink
Post by 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
Ian McCallion
2018-10-27 10:56:04 UTC
Permalink
Post by Andreas Weber
Post by 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
Loading...