Which one is correct ? Using (' ') or @(var)
1 次查看(过去 30 天)
显示 更早的评论
I found that using (' ') can result in the same value on feval(), or other functions like fplot(), instead of using @(x). What is the difference? Also I read documentation and inline(expr) works too, even though it's rather not recommended. For example :
feval('sin',[pi/2,pi])
%or
fplot('2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x)',[-5 5], '--r')
%or example for @()
feval(@(x) sin(x),[pi/2,pi])
fplot(@(x) 2*x^3+2.*cos(2*x).*sin(2*x)+3.*tan(2*x),[-5 5], '--r')
2 个评论
Image Analyst
2021-11-8
编辑:Image Analyst
2021-11-8
I don't understand the question. Where are you using either '' or @????
Exactly what is not recommended? Reading the documentation . . . or some particular function like feval() or fplot()?
采纳的回答
Walter Roberson
2021-11-8
编辑:Walter Roberson
2021-11-8
If you pass in a character vector to fplot, then fplot will
- test whether the input has no names in it (such as if it is a quoted numeric constant). In this case, it will convert the number into an anonymous function that returns a copy of the constant the same size as the input;
- test whether the vector is the name of a built-in function, or there is an executable file by that name, and if so will build a function handle from the name.
- Otherwise, it will vectorize() the character vector and build an anonymous function using the default variable ordering returned by symvar() for the parameter names
In all three cases the result is a function handle; a "simple" handle for the second case and an anonymous function for the other two cases.
The difference between that and passing in an anonymous or function handle you built yourself, is that if you built your own, then potentially you did so only once and re-use the handle, whereas fplot() needs to build the function handle each time it is executed. Also, if you pass in a character vector that is the name of an executable function, it must be the name of a "top-level" function -- one that has its own file, not a nested or local function.
The work done by fplot() is a convenience compared to building your own handle.... unless you are referring to a local function or nested function, in which case you need to build your own handle.
2 个评论
Walter Roberson
2021-11-8
编辑:Walter Roberson
2021-11-8
A number of routines such as ode45() that normally expect function handles, internally use feval() to evaluate. Because of that, they can accept character vectors that are the names of built-in functions, or executable functions. However, if you pass in a character vector that is the name of an executable function, it must be the name of a "top-level" function -- one that has its own file, not a nested or local function; if you a referring to a local function or nested function, you need to build your own handle.
It is not specified exactly how feval() works, other than that it "follows the same scoping and precedence rules as calling a function handle directly"
Walter Roberson
2021-11-8
There is one case where passing in a character vector has historically been better than a function handle:
Historically, arrayfun() had a set of built-in character vectors that it could detect and which it could process more efficiently than a function handle. I do not recall now which names they were, other than I recall that 'length' was one of them. arrayfun() was historically implemented as a .m file that you could read the source for. These days, arrayfun() has its functionality built-in, and I do not know if anything is special-cased anymore.
The local expert on getting speedups through that method is @Jan -- perhaps he can tell us whether this is still the case.
更多回答(1 个)
Steven Lord
2021-11-8
Some older functions allow you to specify the name of a function to call. But if you're writing code to use in a recent release of MATLAB, prefer using an anonymous function or a regular function handle.
Don't use inline objects either. Those are very old and have some idiosyncracies that function handles or anonymous functions don't. For instance, composing anonymous functions is easy. Composing inline objects is most certainly not.
f = @(x) 2*x;
g = @(x) x.^2;
h = @(x) f(g(x)); % 2*x.^2
h(5) % 2*5.^2 = 50
f2 = inline('2.*x', 'x');
g2 = inline('x.^2', 'x');
h2 = inline('f(g(x))', 'f', 'g', 'x')
h2(f2, g2, 5) % Yes, you have to pass f2 and g2 into h2
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!