Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

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

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?

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...

Upvotes: 4

Views: 314

Answers (1)

jrook
jrook

Reputation: 3519

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.

Upvotes: 3

Related Questions