While having nested functions, in a parent function, they don't recognise the variables defined in the parent one. Matlab does that and it is really annoying since neither declaring them global works. Please fix.
I just encountered this too. MATLAB and Octave allow nested functions to access variables in the scope of the enclosing function. This makes it possible to define, for example, a non-anonymous function that relies on some input arguments of the enclosing function. For example, the following function is supposed to add scalar to each entry of X:
Please post an example.
Example
Function f=my_function(input)
constant = 5
Function g=eqation(init)
do something with constant
I just encountered this too. MATLAB and Octave allow nested functions to access variables in the scope of the enclosing function. This makes it possible to define, for example, a non-anonymous function that relies on some input arguments of the enclosing function. For example, the following function is supposed to add
scalar
to each entry ofX
:FreeMat will complain that
scalar
is unknown in the scope ofaddentry
. This works, however, if I defineaddentry
as an anonymous function:Of course the functionality of
addscalar
itself is not important here as+
already operates this way.The thing is sometimes the function of which the handle will be passed as an argument to another function cannot be written as an anonymous function.