Why do I receive an error when creating a function handle to a class method in MATLAB 7.8 (R2009a)?
1 view (last 30 days)
Show older comments
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
Accepted Answer
MathWorks Support Team
on 27 Jul 2009
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 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!