Wed 19 Feb 2014 01:57:21 AM UTC, comment #11:
Sorry, this is what I meant:
|
Tue 18 Feb 2014 10:25:24 PM UTC, comment #10:
Clearing subfunctions is a very wrong thing to do. They should be on the same footing as the parent function. Clearing functions should just clear their persistent variables, not remove any function from the symbol table (except in the Octave-only case of functions defined in script files instead of function files).
The attached test case works in 3.8.0 but not in 3.6.x
(file #30600, file #30601)
|
Tue 18 Feb 2014 10:20:21 PM UTC, comment #9:
Just curious: was it a bug at all, or is ML's behavior now mimicked just by accident (and do we now have bug-compatibility)?
Reading up on TMW site I find the description of what "clear functions" is supposed to do still a bit ambiguous. About the effects of the "clear" command, more specifically "clear functions", it states more or less:
"...clear from current workspace... ... <Compiled Scripts, Functions, MEX-functions> ..."
which to me appears exactly like the behavior that the OP thinks is an Octave bug. It even rather looks to be a Matlab bug or ML documentation bug.
Or, do I wrongly perceive a subfunction to be a function in the parent function's workspace? (could be, I'm sometimes easily confused :-) )
BTW, in ML r2014a prerelease it still works as per OP's expectations.
|
Tue 18 Feb 2014 08:59:01 PM UTC, comment #8:
This bug existed in 3.6.x but got fixed in 3.8.0. I can't figure out which cset fixed it, though.
|
Tue 09 Oct 2012 08:05:13 PM UTC, comment #7:
First of all, sorry if my comments sound pedantic.
"what does the behavior with respect to persistent variables have to do with anything?" - Well, in bottom of comment #4 you came up yourself with persistent vars in relation to what you thought that "clear('functions')" would do.
I assume that "clear('functions')" isn't meant (in the sense of: "has never been foreseen") to be used from inside a function. What advantage would "clear('functions')" from within a function have anyway, compared to a simple "clear" in the same context?
But apparently you found some good use for ML's unspecified but apparent behavior that doesn't work the same in Octave, and based on that you now conclude that Octave is "incompatible" or even behaves "nonsensically" (no offense taken BTW).
I'm not gonna repeat this forever:
Octave does strive for "ML compatibility", but for documented functionality only (and BTW, not for bug-for-bug compatibility).
Now, where in the ML docs is the description of "clear('functions')" invoked from within a function?
As to nonsensical: I can imagine a perhaps far-fetched situation where Octave's behavior could be very useful and rational. It just depends.
Sorry if Octave doesn't do what you -based on no formal description at all- expect it to do.
|
Tue 09 Oct 2012 06:23:23 AM UTC, comment #6:
Philip, what does the behavior with respect to persistent variables have to do with anything? I do not understand what difference you interpret into 'clearing' vs. 'reinitializing' persistent variables. But anyway, Octave's behavior with respect to persistent variables is quite correct and consistent with Matlab. That includes calling clear('functions') from within or from without a function.
When I say the expected behavior is to clear persistent variables, then I am asserting that what is unexpected is that: "Calling clear('functions') in a function causes all subfunctions to become undefined."
So let me just put that in different words again: If I call clear('functions') from within a function, Octave does not only "reinitialize persistent variables", but it removes all subfunctions in that same function from the namespace. Those subfunctions cannot be used any more (at least not in the same call to that function).
Nothing in the Matlab documentation can be construed as a specification of such behavior, and regardless of Matlab compatibility it is quite nonsensical to do this.
|
Mon 08 Oct 2012 07:31:21 PM UTC, comment #5:
Thanks for your clarification - I misunderstood, sorry. But it doesn't help your case.
You say: "The expected behavior for clear('functions') is for it to clear persistent variables of every function."
But ML docs (http://www.mathworks.nl/help/matlab/ref/clear.html) clearly state that "clear" (and not "clear('functions')" in a function reinitializes (and not "clears") persistent variables (no pun intended).
(Furthermore, given the context in the relevant stanza I would read that as "...<persistent variables> in that function". The ML docs are -as is often the case- ambiguous.)
More specifically, the ML docs do NOT explicitly specify what the effect of "clear('functions')" is supposed to be when invoked from within a function.
The behavior you see may be caused by unintended locking of a subfunction name symbol in the course of parsing the function file, perhaps it's even a ML bug, who knows.
Lacking an unambiguous description in the ML docs, this is simply called "undocumented behavior" and there's little chance this will be explicitly mimicked in Octave.
If you find ML docs stating what "clear('functions')" within a function does, this bug can be reopened.
|
Mon 08 Oct 2012 07:20:53 AM UTC, comment #4:
My original example does not involve nested subfunctions. The syntax without explicit 'end' for any function is interpreted in the same way as explicit 'end' between every function. Therefore, your example is equivalent to mine.
For the record:
Octave 3.4.3:
> subfunction
error: `subfunction_1' undefined near line 3 column 1
error: called from:
error: /.../subfunction.m at line 3, column 1
Matlab R2011b:
> subfunction
hi
I am not planning on using nested subfunctions, so I do not care much about that.
The expected behavior for clear('functions') is for it to clear persistent variables of every function.
|
Sun 07 Oct 2012 05:07:09 PM UTC, comment #3:
Oopsie, should be:
What's the output of the following (in both Octave and Matlab):
|
Sun 07 Oct 2012 05:05:50 PM UTC, comment #2:
(Changed title for you.)
What effect do you expect from "clear functions"?
FYI, Octave -unlike Matlab- does not have nested subfunctions implemented (yet) (although I think it is in the dev version now).
What's the output of the following (in both Octave and Matlab):
? (note the two end statements - in Octave we'd rather use "endfunction" but with "end" you can also run it in Matlab)
|
Sun 07 Oct 2012 02:15:40 PM UTC, comment #1:
Copy&paste error, sorry. The title was supposed to be
clear('functions') deletes subfunctions
|
Sun 07 Oct 2012 02:13:39 PM UTC, original submission:
Tested on computer('arch'): mingw32-i686 and gnu-linux-x86_64.
Calling clear('functions') in a function causes all subfunctions to become undefined.
$ cat <<EOF >subfunction.m
function subfunction()
clear functions;
subfunction_1();
function subfunction_1()
fprintf('hi\n');
EOF
$ octave
> subfunction
error: `subfunction_1' undefined near line 4 column 1
error: called from:
error: C:\...\subfunction.m at line 4, column 1
Expected output is 'hi'. (Tested with Matlab R2011b)
|