MATLAB 帮助中心
基于函数句柄构造字符向量
c = func2str(fh)
c = func2str(fh) 构造字符向量 c,该字符向量包含与函数句柄 fh 相关联的函数的名称。如果 fh 与匿名函数相关联,则 func2str 返回表示该匿名函数的字符向量。
fh
c
func2str
示例
全部折叠
为 cos 函数和一个匿名函数创建函数句柄,然后将这些函数句柄转换为字符向量。
cos
fh = @cos; c = func2str(fh)
c = 'cos'
fh = @(x,y)sqrt(x.^2+y.^2); c = func2str(fh); disp(['Anonymous function: ' c])
Anonymous function: @(x,y)sqrt(x.^2+y.^2)
创建一个为单个输入计算函数句柄的函数。
在您的工作文件夹下的 evaluateHandle.m 文件中创建以下函数。
evaluateHandle.m
function evaluateHandle(fh,x) y = fh(x); str = func2str(fh); disp('For input value: ') disp(x) disp(['The function ' str ' evaluates to:']) disp(y) end
使用函数句柄计算 pi/2 处的 sin 函数。
pi/2
sin
fh = @sin; x = pi/2; evaluateHandle(fh,x)
For input value: 1.5708 The function sin evaluates to: 1
使用函数句柄计算指定矩阵 A 的 。
A
fh = @(x) x.^2+7; A = [1 2;0 1]; evaluateHandle(fh,A)
For input value: 1 2 0 1 The function @(x)x.^2+7 evaluates to: 8 11 7 8
要转换为字符向量的句柄,指定为函数句柄。
如果您使用 func2str 将函数句柄转换为字符向量,然后使用 str2func 将其重新转换回句柄,将丢失该函数句柄中原来存储的变量。
str2func
全部展开
backgroundPool
ThreadPool
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
在 R2006a 之前推出
str2func | functions
functions
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处