简体   繁体   English

为什么需要Matlab函数“feval”?

[英]Why is the Matlab function “feval” needed?

A function handle can be used to call a function, eg函数句柄可用于调用函数,例如

f = @sin;
val = f(1.0);

so why is feval ever needed?那么为什么需要feval呢?

val = feval(f, 1.0);

When is it useful?什么时候有用?

For feval , the first argument can be a quoted string, giving you more flexibility than a function handle.对于feval ,第一个参数可以是带引号的字符串,比函数句柄更灵活。 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);你可以做一些事情,比如让函数的基本名称后跟fn1fn2fn3 ,然后使用feval(['fn', num2str(k)], x, y, z);动态调用这些feval(['fn', num2str(k)], x, y, z); . .

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.直到14版(MATLAB 7),feval是评价功能手柄...或者是由他们的名字,他们指定的评估函数方法

Source: Mental model for feval() .来源: feval()心理模型

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 ...另一个例子是,例如,我想定义一个带有用户输入函数句柄 f 的绘图函数,因此它必须以某种方式灵活......

pfun=@(f,x) plot(f(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM