# HG changeset patch # User John W. Eaton # Date 1368809804 14400 # Node ID bc79ac595a05252500f7ddde73f349414a3af05f # Parent 092d0a685546b82ab6f333de1006fb6c1e03fe77 allow abbreviations for optimset and optimget (bug #38999) * optimset.m, optimget.m: Handle abbreviated keys and warn for ambiguous abbreviations. New tests. diff -r 092d0a685546 -r bc79ac595a05 scripts/optimization/optimget.m --- 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")); diff -r 092d0a685546 -r bc79ac595a05 scripts/optimization/optimset.m --- 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%")) -