Reputation: 99
So I got this function:
function M = getA(X)
global h;
QPL96 = h;
M = QPL96;
endfunction
Now:
octave:115> h
h = 0.10000
octave:116> getA(X)
ans = [](0x0)
Isn't that strange? Works as long as there isn't an expression involving h. Otherwise returns that garbage. Why can't I do this? Must I work around it by making h an argument?
Upvotes: 3
Views: 2275
Reputation: 13091
You'll have to declare h
as global everywhere you want to use the "global" h
, and that includes your main body. So type global h
at the prompt and you'll be fine. See the documentation. The following works fine for me:
octave> function M = getA(X)
> global h;
> M = h;
> endfunction
octave> h = 0.01
h = 0.0010000
octave> getA
ans = [](0x0)
octave> global h
octave> h
h = [](0x0)
octave> h = 0.01
h = 0.010000
octave> h
h = 0.010000
octave> getA
ans = 0.010000
But really, you shouldn't use global variables, that's really bad practice. Pass the variable as argument. If you find yourself passing the same group of variables, pass a struct, but still don't use global variables.
EDIT: this is the same question.
Upvotes: 3