[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Finding the name of symbol
From: |
John W. Eaton |
Subject: |
Finding the name of symbol |
Date: |
Wed, 7 May 1997 00:04:30 -0500 |
On 5-May-1997, Craig Earls <address@hidden> wrote:
| Is there a function that returns the name of the variable passed to it
| as a string, ala
|
| fooname=SymbolToString(ThisFunc);
|
| fooname="ThisFunc"
After some discussion, I decided to make it possible to get the names
of arguments that are passed to a function. I think this is all that
Craig is really after here, because it seems rather pointless to write
something like
symbol_name (x)
if it is only going to return "x". But it can sometimes be useful to
be able to get at the names of the variables (or even the text of the
expressions) that were used to call a function. For example, I plan
to use this feature to improve the default legends for plots.
Here is the current version of the NEWS entry:
* Within user-defined functions, the new automatic variable `argn'
contains the names of the arguments that were passed to the
function. For example,
function f (...)
for i = 1:nargin
stdout << "argn(" << i << ") = `" << deblank (argn(i,:)) \
<< "' and its value is " << va_arg () << "\n";
endfor
endfunction
f (1+2, "foo", sin (pi/2))
prints
argn(1) = `1 + 2' and its value is 3
argn(2) = `"foo"' and its value is foo
argn(3) = `sin (pi / 2)' and its value is 1
on the standard output stream. If nargin is zero, argn is not defined.
Oh, this example also points out another new feature. :-)
The names are cached when the function call is parsed, so there is
negligible run-time penalty for this feature. The only added run-time
cost is that the local variable argn is currently defined even if it
is not actually used in the function. Eliminating that would take
some work, and there are probably much better projects to work on...
As always, comments and suggestions for improvements are welcome, etc.
Thanks,
jwe