changeset 16674:bc79ac595a05

allow abbreviations for optimset and optimget (bug #38999) * optimset.m, optimget.m: Handle abbreviated keys and warn for ambiguous abbreviations. New tests.
author John W. Eaton <jwe@octave.org>
date Fri, 17 May 2013 12:56:44 -0400
parents 092d0a685546
children 558e1ce7247b
files scripts/optimization/optimget.m scripts/optimization/optimset.m
diffstat 2 files changed, 29 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/optimization/optimget.m	Tue May 07 22:56:42 2013 +0800
+++ b/scripts/optimization/optimget.m	Fri May 17 12:56:44 2013 -0400
@@ -33,12 +33,18 @@
   endif
 
   opts = __all_opts__ ();
-  idx = lookup (tolower (opts), tolower (parname), "m");
+  idx = strncmpi (opts, parname, numel (parname));
+
+  nmatch = sum (idx);
 
-  if (idx)
+  if (nmatch == 1)
     parname = opts{idx};
+  elseif (nmatch == 0)
+    warning ("unrecognized option: %s", parname);
   else
-    warning ("unrecognized option: %s", parname);
+    fmt = sprintf ("ambiguous option: %%s (%s%%s)",
+                   repmat ("%s, ", 1, nmatch-1));
+    warning (fmt, parname, opts{idx});
   endif
   if (isfield (options, parname))
     retval = options.(parname);
@@ -50,3 +56,12 @@
 
 endfunction
 
+%!error optimget ()
+
+%!shared opts
+%! opts = optimset ("tolx", 0.1, "maxit", 100);
+%!assert (optimget (opts, "TolX"), 0.1);
+%!assert (optimget (opts, "maxit"), 100);
+%!assert (optimget (opts, "MaxITer"), 100);
+%!warning (optimget (opts, "Max"));
+%!warning (optimget (opts, "foobar"));
--- a/scripts/optimization/optimset.m	Tue May 07 22:56:42 2013 +0800
+++ b/scripts/optimization/optimset.m	Fri May 17 12:56:44 2013 -0400
@@ -131,17 +131,20 @@
     fnames = fieldnames (old);
     ## skip validation if we're in the internal query
     validation = ! isempty (opts);
-    lopts = tolower (opts);
     for [val, key] = new
       if (validation)
         ## Case insensitive lookup in all options.
-        i = lookup (lopts, tolower (key));
+        i = strncmpi (opts, key, length (key));
+        nmatch = sum (i);
         ## Validate option.
-        if (i > 0 && strcmpi (opts{i}, key))
-          ## Use correct case.
-          key = opts{i};
+        if (nmatch == 1)
+          key = opts{find (i)};
+        elseif (nmatch == 0)
+          warning ("unrecognized option: %s", key);
         else
-          warning ("unrecognized option: %s", key);
+          fmt = sprintf ("ambiguous option: %%s (%s%%s)",
+                         repmat ("%s, ", 1, nmatch-1));
+          warning (fmt, key, opts{i});
         endif
       endif
       old.(key) = val;
@@ -165,6 +168,7 @@
 
 %!assert (optimget (optimset ("tolx", 1e-2), "tOLx"), 1e-2)
 %!assert (isfield (optimset ("tolFun", 1e-3), "TolFun"))
+%!warning (optimset ("Max", 10));
+%!warning (optimset ("foobar", 13));
 
 %!error (optimset ("%NOT_A_REAL_FUNCTION_NAME%"))
-