函数句柄(即
class function_handle的对象)有一个名为
functions的方法,它将返回有关句柄的信息,包括相关文件的完整路径:
>> fs = functions(h)
fs =
function: 'bar'
type: 'simple'
file: 'C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m'
>> fs.file
ans =
C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m
由于函数的输出是结构,因此可以使用getfield在单个命令中完成:
>> fName = getfield(functions(h),'file')
fName =
C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m
但是,如果将它们串在一起,则可以使用func2str和which获取文件名:
>> h = @bar;
>> fName = which(func2str(h))
fName =
C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m