成员函数的Matlab函数句柄
考虑以下Matlab类的定义:成员函数的Matlab函数句柄,matlab,Matlab,考虑以下Matlab类的定义: classdef myClass < handle properties cnn factor end methods function obj = myClass() if nargin>0 obj.cnn = CNN(); obj.factor = Factor(); featureHandle = @obj.cnn.
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
但是上面的代码不起作用,因为Matlab似乎在Factor类中查找属性cnn,可能是因为句柄使用obj引用当前类:
Undefined function 'obj.cnn.getFeature' for input arguments of type 'double'.
我找到了我自己问题的答案: 而不是使用成员函数的直接句柄
featureHandle = @obj.cnn.getFeature;
featureHandle = @(input)(obj.cnn.getFeature(input));
我们需要使用一个匿名函数来模拟对成员函数的调用
featureHandle = @obj.cnn.getFeature;
featureHandle = @(input)(obj.cnn.getFeature(input));
我不确定区别在哪里,但我认为第二个版本强制Matlab实例化obj.cnn.getFeature的副本,这样即使不引用obj也可以调用它。可能是的副本,但代码中可能有错误。请发布完整代码和错误消息。分两步尝试。localcnn=obj.cnn;featureHandle=@localcnn.getFeature@疯狂的老鼠:我知道这篇文章,那里提出的解决方案是正确的。但是,它不处理在另一个对象类中使用句柄的情况,这可能是我的问题的原因。不幸的是,由于IP问题,我不能发布完整的代码,但我已经用错误消息更新了帖子。你能发布
getFeature
的“原型”吗?我的意思是,它是否需要一些输入?是的,它确实需要一个输入,本质上是一个wxhx3
uint8数组(一个RGB图像)。