函数句柄是Matlab的一种常见数据类型。
函数句柄创建使用 @ 或者 str2func()来生成函数句柄。
>> %创建一个cos()函数句柄 >> %方法1:handle=@functionname >> hcos=@cos hcos = @cos >> %方法2:fh = str2func('cos') >> fh = str2func('cos') fh = @cos >> functions(hcos) %使用functions对其进行检测 ans = function: 'cos' type: 'simple' file: '' >> functions(fh) %使用functions对其进行检测 ans = function: 'cos' type: 'simple' file: '' >> isa(hcos,'function_handle') %使用isa进行检测 ans = 1 >> isa(fh,'function_handle') %使用isa进行检测 ans = 1 >> whos %查看句柄的所有信息 Name Size Bytes Class Attributes fh 1x1 16 function_handle hcos 1x1 16 function_handle函数句柄的基本用法举例:
>> sqrt=@(x,y)(x^y) %定义函数句柄,第一个括号中为变量,第二个括号为表达式 sqrt = @(x,y)(x^y) >> a=sqrt(2,5) %直接调用实现运算 a = 32
原文地址:http://blog.csdn.net/z1137730824/article/details/39185191