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

조회 수: 9 (최근 30일)
Naor Movshovitz
Naor Movshovitz 2017년 4월 28일
편집: per isakson 2017년 12월 21일
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

채택된 답변

Guillaume
Guillaume 2017년 4월 28일
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
Guillaume
Guillaume 2017년 4월 29일
편집: per isakson 2017년 12월 21일
Re: documentation. Well, at least it is documented in all versions, so if a change occurs you'll know about it. Saying that, I'm not aware of any major change to the behavior of functions. If you keep with your current design, this is certainly what I'd use.
As Steven says, that design sounds a bit iffy. The way I would implement your requirements would be with functions objects (i.e. classes). I would have an abstract base class from which all functions derive. It is therefore trivial to check whether a function is allowed with isa (or validateattribute) based on its class. To differentiate between the two kind of functions, I would have two additional abstract classes (deriving from the main abstract base class). The whole thing would be something like:
classdef processingfunction
methods (Abstract)
function result = dosomething(this, arg); %the actual method equivalent to the function handles
end
end
classdef likelihoodfunction < processingfunction
%does not need anything extra above the base class. Just a new type
end
classdef lossesfunction < processingfunction
%does not need anything extra above the base class. Just a new type
end
classdef someactualfunction < likelihoodfunction
methods
function result = dosomething(this, arg)
%actual implementation
result = arg + 2;
end
end
end
function find_best(myset, func)
validateattributes(fun, {'processingfunction'}, {'scalar'}); %error if func is not derived from processing function
if isa(func, 'lossesfunction') %switch behaviour depending of subtype
result = func.dosomething(1);
else
result = func.dosomething(2);
end
end
There are other advantages to using function objects, such as caching of inputs, precalculating expensive parameters and possibly others. The downside is that OOP in matlab is not always very fast.
Naor Movshovitz
Naor Movshovitz 2017년 4월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 4월 29일
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
Image Analyst
Image Analyst 2017년 4월 29일
I got the impression that he "needs to branch depending on whether that function is part of +package1 or +package2" so I figured that if he could figure out the folder where that function lives, then he'd know which package it belonged to. That was my thinking.
Naor Movshovitz
Naor Movshovitz 2017년 4월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by