Is it possible to get back a name of a function as a string?

24 次查看(过去 30 天)
Is it possible to get back a name of a function as a string? For example I have myf = @(x) x^2; And I need 'myf'
  2 个评论
Stephen23
Stephen23 2017-12-20
编辑:Stephen23 2017-12-20
"Is it possible to get back a name of a function as a string?"
Yes, it is possible. But introspective programming is slow and inefficient, and should be avoided: "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive."
Magically accessing variables in the workspace is also slow, complex, and very inefficient (no matter how much beginners think that it is a great idea):
Instead of combining two inefficient and complex ways of writing code, why not simply write better code? If you tell us what you are trying to achieve, then we can show you better way of doing it. For example, did you know that function handles can be stored in a cell array and accessed very efficiently using indexing?
Guillaume
Guillaume 2017-12-20
I'm not even sure what is being asked here. myf is not a function. It is a function handle, a completely different thing. The function referenced by that handle is an anonymous function.
Note:
myf = @(x) x^2;
myg = myf;
myg and myf reference the exact same function, a function which again has no name.

请先登录,再进行评论。

回答(3 个)

the cyclist
the cyclist 2017-12-19
编辑:the cyclist 2017-12-19
Use the whos command.
Depending on what is in your workspace, you may need to do some work to narrow down to exactly what you want, for example by selecting only items of class "function_handle".

Vignesh
Vignesh 2017-12-19
编辑:Vignesh 2017-12-19
The function you need is inputname. It returns the name of the function argument, passed in.
inputNameFun = @(x)(inputname(1));
inputNameFun(myf)

Walter Roberson
Walter Roberson 2017-12-19
If you are in the middle of executing a function and you need to know the name of the function, then you need to use dbstack(). Note that anonymous functions do not always have names.
If you have the handle to a function but do not know the name(s) of the variable(s) that have been assigned that function handle, then you can use whos to identify the names of variables that might hold the handle, and then you can loop through the names testing with isequal() to see if they are the same handle. Note that with anonymous functions it is not enough to test whether char() of the two functions is the same, because their workspaces might be different:
a = 1;
f1 = @(x) x + a;
a = 10;
f2 = @(x) x + a;
f3 = f1;
Now strcmp(char(f1), char(f2)) would be the same, but they are not the same functions: is equal(f1, f2) will be false -- but isequal(f1, f3) would be true as they are the same function handle.
If you do take this route of searching the variables for candidates, then remember that even if the anonymous function was assigned to a variable at some point, it might have been assigned to a field or a cell array
test.f1 = @(x) x + a;
test2{1} = @(x) x + a;
so if it is important to find the name of a variable storing the function handle, you might have to search field of every variable, along with every field of every variable in an outer function this one is nested in, along with every parameter passed to this function, along with many possible properties of graphics objects, along with the timer objects, and serial objects, and udp and tcp objects, and along with the listeners, and so on.
... all of which should suggest that searching for "the" name of an anonymous function whose handle you have is probably not something you should do unless you have Very Good Reason.

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by