# HG changeset patch # User John W. Eaton # Date 1544669076 18000 # Node ID c0ac6fc191d7074c2e728164fc16802300553870 # Parent ff0eadb417ec2886a8ff280c0c175f33351ef933 allow inputname to return non-ID arguments (bug #55213) * inputname.m: New optional argument, IDS_ONLY. Default value is true. If false, return text of argument, even if it is not a valid variable name. * assert.m, test.m: Call inputname with new IDS_ONLY argument = false. diff -r ff0eadb417ec -r c0ac6fc191d7 scripts/miscellaneous/inputname.m --- a/scripts/miscellaneous/inputname.m Wed Dec 12 17:22:08 2018 -0800 +++ b/scripts/miscellaneous/inputname.m Wed Dec 12 21:44:36 2018 -0500 @@ -21,6 +21,7 @@ ## -*- texinfo -*- ## @deftypefn {} {} inputname (@var{n}) +## @deftypefnx {} {} inputname (@var{n}, @var{ids_only}) ## Return the name of the @var{n}-th argument to the calling function. ## ## If the argument is not a simple variable name, return an empty string. As @@ -29,12 +30,17 @@ ## ## @code{inputname} is only useful within a function. When used at the command ## line it always returns an empty string. +## +## By default, return an empty string if the @var{n}-th argument is not +## a valid variable name. If the optional argument @var{ids_only} is +## false, return the text of the argument even if it is not a valid +## variable name. ## @seealso{nargin, nthargout} ## @end deftypefn -function name = inputname (n) +function name = inputname (n, ids_only = true) - if (nargin != 1) + if (nargin > 2) print_usage (); endif @@ -47,7 +53,7 @@ ## For compatibility with Matlab, ## return empty string if argument name is not a valid identifier. - if (! isvarname (name)) + if (ids_only && ! isvarname (name)) name = ""; endif diff -r ff0eadb417ec -r c0ac6fc191d7 scripts/testfun/assert.m --- a/scripts/testfun/assert.m Wed Dec 12 17:22:08 2018 -0800 +++ b/scripts/testfun/assert.m Wed Dec 12 21:44:36 2018 -0500 @@ -88,7 +88,7 @@ || isempty (cond) || ! all (cond(:))) if (nargin == 1) ## Perhaps, say which elements failed? - argin = ["(" inputname(1) ")"]; + argin = ["(" inputname(1, false) ")"]; error ("assert %s failed", argin); else error (varargin{:}); @@ -403,7 +403,7 @@ if (! isempty (err.index)) arg_names = cell (nargin, 1); for i = 1:nargin - arg_names{i} = inputname (i); + arg_names{i} = inputname (i, false); endfor argin = ["(" strjoin(arg_names, ",") ")"]; if (! isempty (errmsg)) diff -r ff0eadb417ec -r c0ac6fc191d7 scripts/testfun/test.m --- a/scripts/testfun/test.m Wed Dec 12 17:22:08 2018 -0800 +++ b/scripts/testfun/test.m Wed Dec 12 21:44:36 2018 -0500 @@ -815,7 +815,7 @@ ## Create structure with fieldnames the name of the input variables. function s = var2struct (varargin) for i = 1:nargin - s.(inputname (i)) = varargin{i}; + s.(inputname (i, true)) = varargin{i}; endfor endfunction