Overloaded method defined in class definition, not working

2 views (last 30 days)
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
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?
Muhammad Aneeq uz Zaman
Muhammad Aneeq uz Zaman on 28 Sep 2014
Edited: Muhammad Aneeq uz Zaman on 28 Sep 2014
Hi, this is some of my classdef file. I think I do exactly as you did but for some inexplicable reason it doesn't work.
classdef tree
properties (SetAccess = private)
Node = [];
Parent = [ 0 ];
end
methods
function [obj] = tree(content)
obj.Node = content;
root_ID = 1;
end
function I = find(obj, f)
val = obj.Node ;
I = strmatch(f',val');
end
end
end
And I use it as
t.find(t, 2) or t.find( 2)
Gives me "Error using tree/find Method 'find' is not defined for class 'tree' or is removed from MATLAB's search path."

Sign in to comment.

Accepted Answer

per isakson
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

More Answers (1)

Muhammad Aneeq uz Zaman
Muhammad Aneeq uz Zaman on 28 Sep 2014
Ok I restarted MATLAB and for some weird reason, its working. Thanks for the help guys.
  1 Comment
Geoff Hayes
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.

Sign in to comment.

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!