bugGNU Octave - Bugs: bug #52568, str2func can't create a...


bug #52568: str2func can't create a "lazy" handle to function which does not exist when str2func is executed

Submitter:  Johannes Pfeifer <jpfeifer>
Submitted:  Fri 01 Dec 2017 08:45:26 AM UTC
Category:  Octave Function Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Johannes Pfeifer Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


( Jump to the original submission )

Tue 19 Mar 2019 03:59:36 PM UTC, comment #13: 

I believe this bug was also fixed when bug #29447 was fixed.  I now see the following behavior with the development sources:

octave> fh = str2func ("jskafljsdaklfjsadklfjasd")
fh = @jskafljsdaklfjsadklfjasd
octave:6> fh()
error: jskafljsdaklfjsadklfjasd: invalid function handle

John W. Eaton <jwe>
Project Administrator
Wed 27 Feb 2019 10:30:56 AM UTC, comment #12: 

Andy: Most people working on Octave are volunteers.  Can you help fix this problem?  Or perhaps hire someone to do it?


John W. Eaton <jwe>
Project Administrator
Wed 27 Feb 2019 06:41:26 AM UTC, comment #11: 

This bug should have a higher priority.  Octave's functionality is incompatible with MATLAB.  An error should not issue until the function is invoked.

But MATLAB does NOT allow you to put in ANY string without erroring immediately.  E.g. (in 2012a)

fcn_h = str2func('Some.Cra?zyName');
Warning: The input to STR2FUNC "Some.Cra?zyName" is not a valid function name. This will generate an error in a future release.

Perhaps a documented Octave setting(s) could be created to specify what should happen.  By default, Octave should do what MATLAB does.

Andy Potvin <afpotvin>
Tue 03 Apr 2018 02:24:32 PM UTC, comment #10: 

Yes. Your intuition is correct. Result in Matlab 2017b:

1. cd /dir/without/foo.m (actually foo.m does not yet exist)

2. fh = @foo  %% no foo.m in current dir or loadpath

fh =

  function_handle with value:


3. fh ()      %% presumably an error

Undefined function or variable 'foo'.

4. cd /dir/with/foo.m (I create foo.m in current folder)

5. fh ()      %% calls function defined in /dir/with/foo.m?

this is function fh (just a dummy function that displays this msg)

6. cd /dir/without/foo.m

7. fh ()      %% error or calls previous foo.m?

'foo' is not found in the current folder or on the MATLAB path, but exists in:

Change the MATLAB current folder or add its folder to the MATLAB path.

avlas <kupiqu>
Tue 03 Apr 2018 02:11:24 PM UTC, comment #9: 

What does Matlab actually do with a handle to a nonexistent function?  Is it possible for the handle to become valid, for example if you cd to a directory where the named function exists?  Once it is valid, does it stay defined the same way as a function handle that is valid when it is created?  For example, what does Matlab do for this:

cd /dir/without/foo.m
fh = @foo  %% no foo.m in current dir or loadpath
fh ()      %% presumably an error
cd /dir/with/foo.m
fh ()      %% calls function defined in /dir/with/foo.m?
cd /dir/without/foo.m
fh ()      %% error or calls previous foo.m?

If the last line is an error, then I guess a function handle to a n initially non-existent function behaves more or less like an alias?

John W. Eaton <jwe>
Project Administrator
Tue 20 Mar 2018 01:23:21 PM UTC, comment #8: 

I would encourage you to please reconsider... It may be a Matlab bug, depending how you feel about it, but please note that it can certainly affect negatively to Octave users (http://savannah.gnu.org/bugs/?53363):

If a matlab user saves one of those function handles in a mat file together with other relevant data, an octave user will not be able to access that data because octave fails to load the mat file due to this problem with function handles.

avlas <kupiqu>
Mon 04 Dec 2017 12:56:11 PM UTC, comment #7: 

See also bug #31821

Guillaume <gyom>
Sun 03 Dec 2017 10:51:30 AM UTC, comment #6: 

Apologies for appearing a bit strict :-) that wasn't my intention.

I can clearly perceive the usefulness of lazy evaluation by str2func in other cases as well.
However, as the thread in comment #3 shows, this soon gets one in a morass of arbitrary choices, inconsistencies and completely undocumented Matlab behavior.

For the time being I'll just lower priority & severity; perhaps one of the core devs wants to close it as "won't fix".

Philip Nienhuis <philipnienhuis>
Project Member
Sat 02 Dec 2017 08:37:33 PM UTC, comment #5: 

@Philip I simply wanted to point out the incompatibility. I find Octave's behavior fully reasonable (if properly documented). Problems like the one I mentioned happen usually only in pathological cases where something else in the code should be handled differently. In my case, the problem occurred with the third-party package Dynare. But I guess its problematic code will soon change, because there is no reason to create the function handle if the function does not exist (https://github.com/DynareTeam/dynare/pull/1572).

Johannes Pfeifer <jpfeifer>
Sat 02 Dec 2017 02:41:47 PM UTC, comment #4: 

I intentionally didn't write it in comment #1 but now that Markus revealed this was discussed before I'll do mention:

Note that this is undocumented Matlab behavior (although less clear-cut). The Matlab docs only mention variable names (+ of course anonymous functions, function names, etc.), but definitely not arbitrary strings.

@Johannes I feel with you, but Octave developers would have a hard time making Octave accept all sloppy Matlab code out there that yet runs fine in Matlab.
OTOH for this specific case, somewhere in the thread dug up by Markus the suggestion was made to just defer parsing str2func's input arg until the handle is really invoked. Maybe that can be acceptable for Octave in the future.

BTW what Matlab package do you refer to? supplied by TMW itself, or by a third party?

Philip Nienhuis <philipnienhuis>
Project Member
Fri 01 Dec 2017 04:42:23 PM UTC, comment #3: 

I think that issue was discussed here [1].

If I am correctly reading what jwe wrote, he doesn't like the way Matlab handles "lazy" function handle evaluation. That probably means that it is unlikely that Octave will adopt it in the future.

I didn't find another bug report about that particular issue. So I'll keep this one open.

[1] http://octave.1599824.n4.nabble.com/Function-handles-for-nonexisting-functions-td1654944.html

Markus M├╝tzel <mmuetzel>
Project Member
Fri 01 Dec 2017 02:32:43 PM UTC, comment #2: 

Thanks for looking into this. My problem is that the check in Octave is stricter than Matlab's, making existing Matlab code incompatible with Octave.

Matlab will of course crash once you invoke the handle to a non-existing function. But I use a Matlab package that creates a handle from an arbitrary string, but does not actually invoke the handle if the function specified by the string does not (yet) exist. Of course this is not optimal, but Matlab has no problem with this sequence of events.

I would simply like Octave to maintain Matlab compatibility, i.e. have str2func return a handle and only throw an error once the handle to something not existing is invoked.

Johannes Pfeifer <jpfeifer>
Fri 01 Dec 2017 01:03:50 PM UTC, comment #1: 

The bug report title doesn't reflect what you write.
AFAIU you mention in the report that Octave's str2func() doesn't want to accept just any string; the title suggests that the input string needs to be the name of an .m file (in Octave's search path I assume).

Anyway, reading up on the Matlab docs, it looks like Matlab should behave the same as Octave, insofar that Matlab also accepts names of variables in the workspace; but once invoked the function handle referred to by str2func's output errors out. Seems fair enough.


  • What exactly do you want to do with the result of your example, 'str2func(["test string"])' ?
  • What does Matlab do when you invoke the function handle from that example?

Apart from the fact that Octave's input check is a little more strict, it seems to me that Octave's behavior is perfectly reasonable.

Release-> dev
Status-> need info

Philip Nienhuis <philipnienhuis>
Project Member
Fri 01 Dec 2017 08:45:26 AM UTC, original submission:  

Matlab allows to use str2func on any string. Octave in contrast throws an error if the resulting function handle does not link to an existing function.


results in

error: @test_string: no function and no method found

while Matlab outputs a function handle

Johannes Pfeifer <jpfeifer>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -email is unavailable- added by afpotvin (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by kupiqu (Posted a comment)
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by jpfeifer (Submitted the item)
  • -email is unavailable- added by jpfeifer

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only project members can vote.





    Follow 12 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-03-19 jwe StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2018-03-18 siko1056 Dependencies- bugs #53363 is dependent
    2017-12-03 philipnienhuis Severity3 - Normal 2 - Minor
        Priority5 - Normal 3 - Low
        StatusNeed Info Confirmed
    2017-12-01 rik5 Carbon-CopyRemoved 72865 -
    2017-12-01 rik5 Summarystr2func erroneously checks for presence of file str2func can't create a "lazy" handle to function which does not exist when str2func is executed
    2017-12-01 philipnienhuis StatusNone Need Info
        Release4.2.1 dev
        Operating SystemMicrosoft Windows Any
    2017-12-01 jpfeifer Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.9