Function handles (i.e. objects of class function_handle
) have a method called functions
, which will return information about the handle, including the full path of the associated file:
>> 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
Since the output of functions
is a struct
, this can be done in a single command with getfield
:
>> fName = getfield(functions(h),'file')
fName =
C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m
However, you can use func2str
and which
to get the file name if you string them together:
>> h = @bar;
>> fName = which(func2str(h))
fName =
C:\Program Files\MATLAB\R2013b\toolbox\matlab\specgraph\bar.m