Luca
Luca

Reputation: 10996

get address/pointer to a class method

I am using Matlab and I want to make a method work with various Matlab versions. So, I have something like:

v = VideoReader('Video.mpg');

% Check if a particular method exists
if any(strcmp(methods(v), 'getFrame'))
   % if the method exists, store it in a variable
end

Now what I want to do is that if this method exists i.e. if the statement above returns true, I want to store a handle to that method in a variable and be able to call it on the VideoReader object. However, I am not sure how to do that.

Upvotes: 1

Views: 39

Answers (1)

beaker
beaker

Reputation: 16801

You can use str2func:

if any(strcmp(methods(v), 'getFrame'))
   % if the method exists, store it in a variable
   h = str2func('getFrame');
end

Then call h the same way you would getFrame.

Upvotes: 1

Related Questions