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; endend%CLASS2
classdef Class2 < handle methods function doNothing(obj) disp('Ok'); end endend
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
Best Answer