Is it possible to get back a name of a function as a string?
조회 수: 23 (최근 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
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
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
2017년 12월 19일
편집: the cyclist
2017년 12월 19일
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".
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!