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

Submitted by:  Johannes Pfeifer <jpfeifer>
Submitted on:  Fri 01 Dec 2017 08:45:26 AM UTC  
Category: Octave FunctionSeverity: 2 - Minor
Priority: 3 - LowItem Group: Matlab Compatibility
Status: ConfirmedAssigned to: None
Originator Name: Johannes PfeiferOpen/Closed: Open
Release: devOperating System: Any

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


(Jump to the original submission Jump to the original submission)

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
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 File(s):

No files currently attached


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -unavailable- added by gyom (Posted a comment)
  • -unavailable- added by mmuetzel (Posted a comment)
  • -unavailable- added by jpfeifer (Submitted the item)
  • -unavailable- added by jpfeifer

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 10 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sun 18 Mar 2018 08:20:13 PM UTCsiko1056Dependencies-=>bugs #53363 is dependent
    Sun 03 Dec 2017 10:51:30 AM UTCphilipnienhuisSeverity3 - Normal=>2 - Minor
      Priority5 - Normal=>3 - Low
      StatusNeed Info=>Confirmed
    Fri 01 Dec 2017 10:29:19 PM UTCrik5Carbon-CopyRemoved 72865=>-
    Fri 01 Dec 2017 10:29:00 PM UTCrik5Summarystr2func erroneously checks for presence of file=>str2func can't create a "lazy" handle to function which does not exist when str2func is executed
    Fri 01 Dec 2017 01:03:50 PM UTCphilipnienhuisStatusNone=>Need Info
      Operating SystemMicrosoft Windows=>Any
    Fri 01 Dec 2017 08:45:26 AM UTCjpfeiferCarbon-Copy-=>Added -unavailable-

    Back to the top

    Powered by Savane 3.1-cleanup1