How to test if a function handle belongs to a particular package?

7 views (last 30 days)
I have a method that accepts a function handle and then needs to branch depending on whether that function is part of +package1 or +package2. While I can think of some hacks involving parsing the function name and/or package's help text and/or output of dir, is there a better or simpler way?
thanks, -n

Accepted Answer

Guillaume on 28 Apr 2017
Probably the simplest would be to parse the output of
The file field of that output will contain the full path to the anonymous function, so will contain the package name, if any.
Naor Movshovitz
Naor Movshovitz on 29 Apr 2017
Thanks both and yes, I agree that using a class instead of a package would help here. One could even use static methods to make the calling syntax identical to the package syntax avoiding rewrites of the calling functions.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Apr 2017
As an example, to find out the folder of the bwdist function in the Image Processing Toolbox:
functionInfo = which('bwdist')
[folder, baseFileNameNoExt, ext] = fileparts(functionInfo)
Here is the file/folder info that is returned:
functionInfo =
'C:\Program Files\MATLAB\R2017a\toolbox\images\images\bwdist.m'
folder =
'C:\Program Files\MATLAB\R2017a\toolbox\images\images'
baseFileNameNoExt =
ext =
Adapt as needed for your function names.
Naor Movshovitz
Naor Movshovitz on 30 Apr 2017
Guillaume is right. There is maybe potential to use which together with evalin but this seems more brittle than using functions. As discussed under that answer a better approach if possible would be to use a class with static methods.

Sign in to comment.


Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!