MATLAB Answers

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

9 views (last 30 days)
Naor Movshovitz
Naor Movshovitz on 28 Apr 2017
Edited: per isakson on 21 Dec 2017
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
Guillaume on 28 Apr 2017
Probably the simplest would be to parse the output of
functions(yourfunctionhandle)
The file field of that output will contain the full path to the anonymous function, so will contain the package name, if any.
  5 Comments
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 =
'bwdist'
ext =
'.m'
Adapt as needed for your function names.
  5 Comments
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.

Community Treasure Hunt

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

Start Hunting!