0

Consider the following definition of a Matlab class:

classdef myClass < handle

properties
    cnn
    factor
end

methods        
    function obj = myClass()
        if nargin>0
            obj.cnn = CNN();
            obj.factor = Factor();
            featureHandle = @obj.cnn.getFeature;
            factor.setCNN(featureHandle);
        end
    end
end

end

cnn and factor are instances of two other classes CNN and Factor. The CNN class has a function getFeature and the Factor class has a function setCNN.

My goal is to let factor use the getFeature method of the CNN class, e.g.,

c = myClass;
c.factor.cnnHandle(input);

But the code above doesn't work, because Matlab seems to look for a property cnn in the Factor class, probably because the handle uses obj to refer to the current class:

Undefined function 'obj.cnn.getFeature' for input arguments of type 'double'.
ASML
  • 199
  • 12
  • Possible duplicate of [this thread](http://stackoverflow.com/questions/7628343/how-to-get-the-handle-of-a-method-in-an-object-class-inst-within-matlab), but with probably an error in your code. please post the full code and the error message. – Ratbert Feb 19 '15 at 20:34
  • 1
    Try it in two steps. localcnn = obj.cnn; featureHandle = @localcnn.getFeature; – Navan Feb 19 '15 at 21:23
  • @Crazy Rat: I know this post and the proposed solution there is correct. However, it doesn't deal with the situation of using the handle inside another object class, which is probably the cause of my problem. Unfortunately, I'm not allowed to post the full code due to IP issues, but I've updated the post with the error message. – ASML Feb 19 '15 at 21:59

1 Answers1

0

I found an answer to my own question:

Instead of using a direct handle of the member function,

featureHandle = @obj.cnn.getFeature;

we need to use an anonymous function that simulates a call to the member function,

featureHandle = @(input)(obj.cnn.getFeature(input));

I'm not sure where the difference is, but I think the second version forces Matlab to instantiate a copy of obj.cnn.getFeature, so that it can be called even without a reference to obj.

ASML
  • 199
  • 12