Finding the name of symbol

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding the name of symbol

Craig Earls-2
Is there a function that returns the name of the variable passed to it
as a string, ala

fooname=SymbolToString(ThisFunc);

fooname="ThisFunc"


--
-----------------------------------------------------------------
Craig P Earls      [hidden email]
LT US Navy, MIT Ocean Engineering      [hidden email]
-----------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: Finding the name of symbol

John Utz-3
Huh?

what are u doing here?

On Mon, 5 May 1997, Craig Earls wrote:

> Is there a function that returns the name of the variable passed to it
> as a string, ala
>
> fooname=SymbolToString(ThisFunc);

this sort of looks like a subfunction of feval.... feval does this to pick
up the function name....but u obviously know that..given your most
excellent job of hacking fsolve...

> fooname="ThisFunc"

umm..( grabs matlab manual ).. int2str on the return of the function?
i dont think that is what u want...maybe u would need to hookinto some
portion of feval and make a call for it

just my very feeble 0.02...
>
> --
> -----------------------------------------------------------------
> Craig P Earls      [hidden email]
> LT US Navy, MIT Ocean Engineering      [hidden email]
> -----------------------------------------------------------------
>
>

*******************************************************************************
 John Utz [hidden email]
        idiocy is the impulse function in the convolution of life


Reply | Threaded
Open this post in threaded view
|

Finding the name of symbol

John W. Eaton-6
In reply to this post by Craig Earls-2
On  5-May-1997, Craig Earls <[hidden email]> 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