How to get the fully-qualified name of the currently executing function?

Viewed 176

Suppose I have:

+MyPackage/+MySubPackage2/some_function.m

How can I generate the string 'MyPackage.MySubPackage2.some_function' from within this some_function.m when it's executing?

  • mfilename(), dbstack(), what(), etc. all just give 'some_function'
  • meta.package.fromName requires the string we're after as its input
  • parsing the full path (mfilename('fullpath')) or meta.package.getAllPackages() etc. seems to be the only way...

Seems that calling mfilename('class') in a class inside a package gives the right answer, but there's no equivalent for plain functions...

...or is there? Feels like I'm missing something obvious...

1 Answers

If it is possible to import the containing package (say p1/p2), then:

function outputArg1 = some_function()
import p1.p2.*
t = @some_function;
func2str(t)
%ans  = 'p1.p2.some_function'
outputArg1 = ...;
end

The method in this answer may also be used (with some changes possibly) to automate the import process.

Related