fcnchk will not accept string expressions for FUN in a future release. Use anonymous functions for FUN instead.
fcnchk函数将会在以后的版本中删除,以后用匿名函数代替
fcnchk(FUN,...) returns an inline object based on FUN if FUN
is a string containing parentheses, variables, and math
operators. fcnchk simply returns FUN if FUN is a function handle,
or a MATLAB object with an feval method (such as an inline object).
If FUN is a string name of a function (e.g. 'sin'), fcnchk returns a
function handle to that function.
fcnchk is a helper function for FMINBND, FMINSEARCH, FZERO, etc. so they
can compute with string expressions in addition to functions.
fcnchk(FUN,...,'vectorized') processes the string (e.g., replacing
'*' with '.*') to produce a vectorized function.
When FUN contains an expression then fcnchk(FUN,...) is the same as
INLINE(FUN,...) except that the optional trailing argument 'vectorized'
can be used to produce a vectorized function.
[F,ERR] = fcnchk(...) returns a struct array ERR. This struct is empty
if F was constructed successfully. ERR can be used with ERROR to throw
an appropriate error message if F was not constructed successfully.
简单点来说,如果将字符串传递给fcnchk函数,函数会返回inline函数(前提是这个字符串代表的函数你以经定义了)
例如我定义了一个矩形函数:
function z = rect(x)
x= abs(x);
z = (x<1/2)*1.0 ;
z(find(x==1/2)) = 0.5;
end
上面的函数是一维的,如果我想定义一个二维的矩形函数怎么办呢?
我可以将字符串rect(x).*rect(y)
传递到fcnchk
函数里,会返回一个定义好了的inline函数func
,可以理解为定义好了的函数句柄,
fun = 'rect(x).*rect(y)'
func = fcnchk(fun);
调用func函数,X,Y是函数的坐标
f = feval(func,X,Y);
如果想要指定输入参数的情况下,例如定义矩形函数的门宽,rect(x/w).*rect(y/w)
,
可以采用下面的形式
fun = 'rect(x/w).*rect(y/w)'
func = inline(fun,'x','y','w');
不过这个函数快要退出历史舞台了,还是去学匿名函数的用法吧😂
看到一个不错的参考资料,需要的朋友可以去看下:
http://pages.cs.wisc.edu/~cs310-1/modules/Programming/_More%20Advanced%20Functions/fcnchk%20Function/TopicDiscussion.html
觉得帮到您的朋友请赏个赞👍呗!