functions
关于函数句柄的信息
说明
示例
创建函数句柄并显示其信息。
fh = @cos; s = functions(fh)
s = struct with fields:
function: 'cos'
type: 'simple'
file: 'MATLAB built-in function'
创建匿名函数的函数句柄。显示其信息和所需变量的值。
创建函数 x2 + y 的句柄,并使用该句柄调用该函数。
y = 7; fh = @(x)x.^2+y; z = fh(2)
z =
11显示有关函数句柄的信息。
s = functions(fh)
s =
function: '@(x)x.^2+y'
type: 'anonymous'
file: ''
workspace: {[1x1 struct]}
within_file_path: '__base_function'函数句柄包含必需的 y 值。
s.workspace{1}ans =
y: 7创建一个函数,该函数返回局部函数和嵌套函数的句柄。显示其信息。
在您的工作文件夹下的 functionsExample.m 文件中创建以下函数。该函数返回嵌套函数和局部函数的句柄。
function [hNest,hLocal] = functionsExample(v) hNest = @nestFunction; hLocal = @localFunction; function y = nestFunction(x) y = x + v; end end function y = localFunction(z) y = z + 1; end
调用该函数。
[hNest,hLocal] = functionsExample(13)
hNest =
@functionsExample/nestFunction
hLocal =
@localFunction显示有关嵌套函数的句柄的信息。
s1 = functions(hNest)
s1 =
function: 'functionsExample/nestFunction'
type: 'nested'
file: 'C:\work\functionsExample.m'
workspace: {[1x1 struct]}显示有关局部函数的句柄的信息。
s2 = functions(hLocal)
s2 =
function: 'localFunction'
type: 'scopedfunction'
file: 'C:\work\functionsExample.m'
parentage: {'localFunction' 'functionsExample'}
输入参数
查询的句柄,指定为函数句柄。
输出参量
有关函数句柄的信息,以结构体形式返回。结构体包含以下字段。
字段名称 | 字段说明 |
|---|---|
| 函数名称。如果与句柄相关联的函数是嵌套函数,则函数名称的形式为 |
| 函数类型。例如, |
| 带有文件扩展名的函数的完整路径。
|
结构体还有其他字段, 具体取决于与该句柄相关联的函数类型。例如,局部函数有 parentage 字段,匿名函数有 workspace 字段。s 中的信息仅用于执行查询和调试操作。
扩展功能
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出
MATLAB Command
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 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)