Is there possibility to retrieve the absolute path to the file containing a function represented by a function handle? For example:
%child folder containing test_fun.m file
handle = @test_fun
cd ..
%root folder - test_fun not available
path = GETPATHFROMHANDLE(handle)
Is there equivalent to GETPATHFROMHANDLE function in MATLAB? It seems to by simple functionality, but I can't work it out. I know about func2str and which functions, but that doesn't work in that case.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With