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