Overloaded method defined in class definition, not working
Ältere Kommentare anzeigen
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 Kommentare
Geoff Hayes
am 28 Sep. 2014
Muhammad - how have you defined your class (with classdef property in file, or another way using the @), how have you defined the find method, and how are you invoking it?
Muhammad Aneeq uz Zaman
am 28 Sep. 2014
Geoff Hayes
am 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
am 28 Sep. 2014
Bearbeitet: Muhammad Aneeq uz Zaman
am 28 Sep. 2014
Akzeptierte Antwort
Weitere Antworten (1)
Muhammad Aneeq uz Zaman
am 28 Sep. 2014
0 Stimmen
1 Kommentar
Geoff Hayes
am 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.
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!