func2str
基于函数句柄构造字符向量
说明
示例
将函数句柄转换为字符向量
为 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
文件中创建以下函数。
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
函数。
fh = @sin; x = pi/2; evaluateHandle(fh,x)
For input value: 1.5708 The function sin evaluates to: 1
使用函数句柄计算指定矩阵 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
输入参数
fh
— 要转换为字符向量的句柄
函数句柄
要转换为字符向量的句柄,指定为函数句柄。
提示
如果您使用
func2str
将函数句柄转换为字符向量,然后使用str2func
将其重新转换回句柄,将丢失该函数句柄中原来存储的变量。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
如果输入参数与某匿名函数关联,则生成的代码将返回字符向量
'@(...)...'
。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)