]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/inputname.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / inputname.m
1 ## Copyright (C) 2004-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18 ##
19 ## Original version by Paul Kienzle distributed as free software in the
20 ## public domain.
21
22 ## -*- texinfo -*-
23 ## @deftypefn {Function File} {} inputname (@var{n})
24 ## Return the name of the @var{n}-th argument to the calling function.
25 ## If the argument is not a simple variable name, return an empty string.
26 ## @end deftypefn
27
28 function s = inputname (n)
29
30   if (nargin == 1)
31     s = evalin ("caller", sprintf ("__varval__ (\".argn.\"){%d};", n));
32     ## For compatibility with Matlab, return empty string if argument
33     ## name is not a valid identifier.
34     if (! isvarname (s))
35       s = "";
36     endif
37   else
38     print_usage ();
39   endif
40
41 endfunction
42
43 ## Warning: heap big magic in the following tests!!!
44 ## The test function builds a private context for each
45 ## test, with only the specified values shared between
46 ## them.  It does this using the following template:
47 ##
48 ##     function [<shared>] = testfn(<shared>)
49 ##        <test>
50 ##
51 ## To test inputname, I need a function context invoked
52 ## with known parameter names.  So define a couple of
53 ## shared parameters, et voila!, the test is trivial.
54 %!shared hello,worldly
55 %!assert(inputname(1),'hello');
56 %!assert(inputname(2),'worldly');