Overloaded method defined in class definition, not working

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 件のコメント

Geoff Hayes
Geoff Hayes 2014 年 9 月 28 日
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
Muhammad Aneeq uz Zaman 2014 年 9 月 28 日
Hi Geoff, I have defined my class in a file with classdef, not in a @-file. I defined the find method in the file, just like the other methods (function I = find(obj, f))
Geoff Hayes
Geoff Hayes 2014 年 9 月 28 日
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 2014 年 9 月 28 日
編集済み: Muhammad Aneeq uz Zaman 2014 年 9 月 28 日
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."

サインインしてコメントする。

 採用された回答

per isakson
per isakson 2014 年 9 月 28 日
編集済み: per isakson 2014 年 9 月 28 日

0 投票

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

その他の回答 (1 件)

Muhammad Aneeq uz Zaman
Muhammad Aneeq uz Zaman 2014 年 9 月 28 日

0 投票

Ok I restarted MATLAB and for some weird reason, its working. Thanks for the help guys.

1 件のコメント

Geoff Hayes
Geoff Hayes 2014 年 9 月 28 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreate System Objects についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by