help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: function nested and global variables : loss reference


From: Kai Torben Ohlhus
Subject: Re: function nested and global variables : loss reference
Date: Fri, 10 May 2019 13:14:26 +0200

On Fri, May 10, 2019 at 12:16 PM LucaLuca via Help-octave <address@hidden> wrote:
hi, look this code::

function Test()

global b;

b=4;
A();

b
function A()

b=7;

endfunction

endfunction


>> Test
error: 'b' undefined near line 8 column 1
error: called from
    Test at line 8 column 1

I need to use variables global but i see i loss reference variables



Your example works in Matlab R2019a Update 1, but not in Octave 5.1.0.  In Octave 6 (the current development version) it will work, thanks to great efforts by jwe and others improving the interpreter.  Thus there is no need to report a bug about it.  I think you have two options:

1. Wait for Octave 6 (maybe next year).
2. Refactor your code to use subfunctions instead of nested functions [1] if possible.  The following works in Octave 5.1.0:

----- File Test.m -----
function Test()
global b;
b=4;
A();
b
endfunction

function A()
global b;
b=7;
endfunction
------------------------------

HTH,
Kai
 
[1] https://octave.org/doc/v5.1.0/Nested-Functions.html

reply via email to

[Prev in Thread] Current Thread [Next in Thread]