# HG changeset patch # User Rik # Date 1443000815 25200 # Node ID df7d34a1c7e61566cee3a3ed91371e05e3d0fcc2 # Parent 2aa4fb60ae77fa7e918152258bc5275de2dfa9d4 str2func: Allow anonymous function string inputs (bug #45682). * ov-fcn-handle.cc (Ffunc2str): Use eval_string to create a function handle when string begins with anonymous function marker '@'. Update docstring. Add BIST tests. diff -r 2aa4fb60ae77 -r df7d34a1c7e6 libinterp/octave-value/ov-fcn-handle.cc --- a/libinterp/octave-value/ov-fcn-handle.cc Tue Sep 22 21:45:24 2015 +0200 +++ b/libinterp/octave-value/ov-fcn-handle.cc Wed Sep 23 02:33:35 2015 -0700 @@ -1854,10 +1854,6 @@ \n\ If the optional @qcode{\"global\"} argument is passed, locally visible\n\ functions are ignored in the lookup.\n\ -\n\ -Note: @code{str2func} does not currently accept strings which define\n\ -anonymous functions (those which begin with @samp{@@}).\n\ -Use @w{@code{eval (@var{str})}} as a replacement.\n\ @seealso{func2str, inline}\n\ @end deftypefn") { @@ -1871,10 +1867,15 @@ std::string nm = args(0).string_value (); if (nm[0] == '@') { - error ("str2func: Can't process anonymous functions."); - return retval; + int parse_status; + octave_value anon_fcn_handle = + eval_string (nm, true, parse_status); + + if (parse_status == 0) + retval = anon_fcn_handle; } - retval = make_fcn_handle (nm, nargin != 2); + else + retval = make_fcn_handle (nm, nargin != 2); } else error ("str2func: FCN_NAME must be a string"); @@ -1886,6 +1887,22 @@ } /* +%!test +%! f = str2func ("<"); +%! assert (class (f), "function_handle"); +%! assert (func2str (f), "lt"); +%! assert (f (1, 2), true); +%! assert (f (2, 1), false); + +%!test +%! f = str2func ("@(x) sin (x)"); +%! assert (func2str (f), "@(x) sin (x)"); +%! assert (f (0:3), sin (0:3)); + +%!error str2func ({"sin"}) +*/ + +/* %!function y = __testrecursionfunc (f, x, n) %! if (nargin < 3) %! n = 0;