Why do I receive an error when creating a function handle to a class method in MATLAB 7.8 (R2009a)?
1 次查看(过去 30 天)
显示 更早的评论
I am defining two classes Class1 and Class2, as follows:
%CLASS1
classdef Class1 < handle
methods
function obj = Class1(obj)
obj.prop1 = Class2();
end
end
properties
prop1;
end
end
%CLASS2
classdef Class2 < handle
methods
function doNothing(obj)
disp('Ok');
end
end
end
When I create an object of Class1, its property ‘prop1’ is an object of Class2; allowing me to invoke the 'doNothing' method.
a = Class1;
a.prop1.doNothing;
This will print ‘Ok’ at the command prompt. But, when I try to create a function handle to this method, MATLAB is not able to interpret this command and throws an error when I use the function handle.
F_handle = @ a.prop1.doNothing;
F_handle();
However this works fine:
F_handle = @ ()a.prop1.doNothing;
F_handle();
b = Class2;
F_handle2 = @b.doNothing
采纳的回答
MathWorks Support Team
2009-7-27
The syntax F_handle = @obj.property.method is not supported in MATLAB 7.8 (R2009a). You need to use the anonymous function syntax instead:
F_handle = @ ()a.prop1.doNothing;
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!