using feval or not?
Show older comments
I have function handle myfun I need to evalute
I prefer using feval https://www.mathworks.com/help/matlab/ref/feval.html
y = feval(myfun, x1, x2)
instead of direct call
y = myfun(x1, x2)
because when reading code in the first syntax I know then myfun is a function handle, and in the second syntax there might be a confusion of what myfun is. So for readability and maintainability I prefer using feval.
Note that myfun is NOT a string or char.
I test the speed and it seems to me there is no speed penalty one way or another.
I even think the presence of feval in the code will help the parser-compiler to have specific code of evaluation this function at the compiation time and not at a decision at the runtime, so in theory feval could be faster.
Is there any argument againts using feval?
9 Comments
Walter Roberson
on 29 Jul 2023
feval() is not a keyword, so the parser still has to do name resolution on it. The difference is that the name resolution would determine that it is a function, potentially allowing earlier optimization than the case where it is not known at parse time whether a name passed in is an array or a function handle.
One thing I do not know is whether if you were to use an argument block to enforce that the parameter is a function handle, then would the parser be able to reason about that to be able to hypothetically do the same kind of optimization described above? ... Though surely the cost of deriving that kind of information from an argument block would be higher than the cost of deriving it from doing name resolution.
Paul
on 29 Jul 2023
Do you use feval only for anonynous functions? Or do you use it also for your own m-functions? Do you ever use it for Matlab built-in or toolbox functions? What about nesting anonymous functions, like f(g(x1,x2),h(x1,x2))?
Never occured to me to use feval for anything, so just curious about your usage.
Bruno Luong
on 29 Jul 2023
Edited: Bruno Luong
on 30 Jul 2023
Rik
on 30 Jul 2023
If I recall correctly, I use this syntax for compatibility reasons, since either old Matlab releases or Octave do not support the direct call syntax. I'm surprised (and glad) to see it makes so little difference.
Bruno Luong
on 30 Jul 2023
Walter Roberson
on 30 Jul 2023
A surprising number of people still code character vectors for the function name for ode45(), ode15s(), or the optimization functions such as fmincon . I would think it likely that Octave would want to maintain compatibility with that possibility.
Rik
on 31 Jul 2023
I was just saying I'm using feval instead of the direct call method. I don't have my installs handy, so I can't look it up for you when the direct call became possible.
At any rate, feval predates anonymous functions (which were introduced in v7). I can't check right now if v6 already had it, but v6.5 definitely does. (I don't extend the compatibility range of my functions to v6, since it doesn't support && and , which would require a lot of extra work in coding, for just a handful of people globally who would benefit)
Bruno Luong
on 31 Jul 2023
Edited: Bruno Luong
on 31 Jul 2023
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!