Overloaded method defined in class definition, not working
2 views (last 30 days)
Show older comments
Muhammad Aneeq uz Zaman
on 28 Sep 2014
Edited: per isakson
on 28 Sep 2014
Hi all,
I defined a method 'find' in a class definition('tree'). Upon doing 'help find' I can see it under the Overloaded Methods heading ('tree/find'). But when I try to use it MATLAB gives me error saying "Method 'find' is not defined for class 'tree' or is removed from MATLAB's search path." Would anyone have any idea what the problem might be? Thanks.
4 Comments
Geoff Hayes
on 28 Sep 2014
If I define a very simple class like
classdef MyClass
properties (Access=private)
data;
end
methods
function obj = MyClass(n)
obj.data = randi(255,n,1);
end
%FIND Returns index into data if f is found in data
function r = find(obj,f)
r = find(obj.data==f);
end
function d = get(obj)
d = obj.data;
end
end
end
I am able to make use of the find function as
myObj = MyClass(42);
myObj.find(111)
The result is either an empty matrix (if 111 is not an element within the data array member of the instance), or the index of 111 in the array.
Are you doing something similar?
Accepted Answer
per isakson
on 28 Sep 2014
Edited: per isakson
on 28 Sep 2014
I made a classdef-file according to you description. It works as expected (R2013a).
>> t= cssm_tree
t =
cssm_tree with no properties.
>> t.find('abc')
cssm_tree/find
ans =
17
>> help find
....
Overloaded methods:
tree/find
mtree/find
cssm_tree/find
...
where
classdef cssm_tree < handle
methods
function ix = find( this, f )
% find is overloaded
disp( 'cssm_tree/find' )
ix = 17;
end
end
end
0 Comments
More Answers (1)
Muhammad Aneeq uz Zaman
on 28 Sep 2014
1 Comment
Geoff Hayes
on 28 Sep 2014
I wonder if you had made changes to the class definition without deleting/clearing any instances of the class in the workspace, and then tried to use that instance again. For example, if I take your tree class as is and create an instance as
t = tree(1);
t.find(1)
I get the "correct" answer of
ans =
1
If I then comment out the find function in the tree class (and save the changes), and try again
t.find(1)
I observe the error
Error using tree/find
Method 'find' is not defined for class 'tree' or is removed from MATLAB's
search path.
Since you restarted and everything worked okay, then perhaps in the future if you make changes to the class definition file (if that was the problem?) then always remember to clear any local variables that are instances of that class. Sometimes you are reminded of this by MATLAB (especially when you add functionality) with the
Warning: The class file for 'tree' has been changed, but the change cannot be applied because objects based on the old class file still exist. If you use those objects, you might get unexpected results. You can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove those objects.
See Also
Categories
Find more on Software Development Tools 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!