Which one is correct ? Using (' ') or @(var)

2 views (last 30 days)
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 Comments
Image Analyst
Image Analyst on 8 Nov 2021
Edited: Image Analyst on 8 Nov 2021
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()?
Aji Bowo
Aji Bowo on 8 Nov 2021
I use ' ' at 'sin' and for @ doesnt we usually use @ for anonymous function? and sorry for that typo i mean i read that code inline() works too but its not recomended it said in documentation

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Nov 2021
Edited: Walter Roberson on 8 Nov 2021
If you pass in a character vector to fplot, then fplot will
  1. 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;
  2. 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.
  3. 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 Comments
Walter Roberson
Walter Roberson on 8 Nov 2021
Edited: Walter Roberson on 8 Nov 2021
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
Walter Roberson on 8 Nov 2021
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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 8 Nov 2021
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
ans = 50
f2 = inline('2.*x', 'x');
g2 = inline('x.^2', 'x');
h2 = inline('f(g(x))', 'f', 'g', 'x')
h2 = Inline function: h2(f,g,x) = f(g(x))
h2(f2, g2, 5) % Yes, you have to pass f2 and g2 into h2
ans = 50

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!