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?
You’ll have to declare
has global everywhere you want to use the “global”h, and that includes your main body. So typeglobal hat the prompt and you’ll be fine. See the documentation. The following works fine for me: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.