A function handle can be used to call a function, e.g.
f = @sin;
val = f(1.0);
so why is fev
For feval
, the first argument can be a quoted string, giving you more flexibility than a function handle. You could do things like having functions with a base name followed by an index as in fn1
, fn2
, fn3
, and then invoke these dynamically using feval(['fn', num2str(k)], x, y, z);
.
Not necessarily always you know what function is supposed to used; there are many examples
here is a silly example: let, in a text file after each number, different word that can be evaluated as a mathematical function, like, mean, min, max, square are represented, and the task is calculating the result after each like,
2,square,2,power,4,log,10 ...
and you want to write a program to go through this text and calculate the final result ...
another example is for example, I want to define a plot function with user input function handle f, so it must be in a way flexible ...
pfun=@(f,x) plot(f(x))
Until Release 14 (MATLAB 7), feval was the way to evaluate a function handle... or to evaluate functions when they are specified by their name.
Source: Mental model for feval().