How to use global variables in several octave scenarios?

Suppose I have three octave scripts a.m, b.m, c.mand two global variables x, y. Is it possible to define these global variables so that they can be shared between scripts? For example, in a separate include file?

In general, how do global variables work in the GNU octave?

+5
source share
4 answers

It seems you need to declare a global variable, and you should also explicitly tell Octave that the variable you are referencing is in a different (global) scope.

in the .m library

global x = 1;

in main.m

function ret = foo()
    global x;
    5 * x;
endfunction

foo () should return 5

+7
source

How to use global variables in an octave:

tmp.m .

global x;   %make a global variable called x
x = 5;       %assign 5 to your global variable
tmp2();     %invoke another function

tmp2.m :

function tmp2()
  %the following line is not optional, it tells this function go go out and get
  %the global variable, otherwise, x will not be available.
  global x 
  x
end

:

octave tmp.m

:

x =  5

x tmp.m tmp2.m

+2

Sort of:

File with global variables globals.musing:

global my_global = 100;

And just use sourceinside each file. For instance:

source globals.m

global my_global

printf("%d\n", my_global);
+2
source

Not sure if this is the best solution, but in the end I created a shell script from which all other scripts are called. I put my global variables inside this shell script

0
source

All Articles