MATLAB Answers

Method is not defined or is removed from MATLAB search path

145 ビュー (過去 30 日間)
abblah abblah
abblah abblah 2019 年 6 月 17 日
編集済み: Danny Kumpf 2020 年 9 月 25 日 21:26
Hi, this error occurs in the new versions of MATLAB (version = 2018b)
This error occurs when you have defined a method in a separate file,
This occurs for the both static and regular methods.
When every thing is correct and even the MATLAB auto-completion finds the method,
but when you call the method, the error "Method is not defined or is removed from MATLAB search path" occurs.
Note that this error is automatically resolved when MATLAB is restarted multiple times!
I think it is a bug in the new versions.

  10 件のコメント

表示 7 件の古いコメント
Paul Rancuret
Paul Rancuret 2020 年 1 月 21 日
Guillaume and Steven Lord,
I apologize, I made a very stupid mistake. I thought the particular 'ourTools' folder I'm describing was on the path, but I had a typo in the script I used to set my path, so it wasn't actually on the path. Only the children of 'ourTools' were actually added to my path (other than the method folder described). So that was my problem all along.
Sorry for wasting your time, and I appreciate that you did look at this for me!
Paul Rancuret
Steven Lord
Steven Lord 2020 年 1 月 21 日
In that case, would it be fair to eliminate the "bug in matlab [sic] path" tag from this question since it didn't turn out to be a bug with the search path?
Danny Kumpf
Danny Kumpf 2020 年 9 月 25 日 21:21
I experienced what I think might be the same issue today.
I have a class folder, say @MyClass, that has various methods; some defined within MyClass.m, and some defined in separate files in the @MyClass folder. Trying to call one of the externally-defined methods on an instance (ie instance.myExternalMethod()) gives me the error, even though the parent directory of @MyClass was on my path:
Error using MyClass/myExternalMethod
Method 'myExternalMethod' is not defined for class 'MyClass' or is removed from MATLAB's search path
The strange parts are that:
  • I've been using the same class, methods, and matlab path for months with no issues. The only potential difference this time was that I was running another MATLAB instance in the background in the same working directory as the one in which I was seeing this problem
  • Methods defined within MyClass.m are working just fine
  • Matlab IS able to auto-complete the names of the class methods that it supposedly can't find. Ie, typing instance.myEx and hitting tab correctly auto-completes to instance.myExternalMethod
  • Examining the path variable shows nothing amiss. The directory containing @MyClass is on the path, and there doesnt seem to be anything on the path that might be confounding things
  • Performing which('MyClass') shows what I'd expect: \\server1\...\@MyClass\MyClass.m % MyClass constructor
  • Performing which('myExternalMethod') shows: \\server1\...\@MyClass\MyClass.m % MyClass method (note: there is a method stub of myExternalMethod in MyClass.m)
So it really looks like MATLAB is finding the method, but for some reason can't call it.
The issue ended up fixing itself, and here's what happened:
  • Since the which() call on the external method returned MyClass.m instead of @MyClass/myExternalMethod.m, I wanted to see what would happen if I commented out the function definition stub in MyClass.m. That is, MyClass.m contains the line "myExternalMethod(obj)", and I commented this out
  • After that, the call to which('myExternalMethod') showed: \\server1\...\@MyClass\myExternalMethod.m as I might expect
  • And now, suddenly, everything was working again. I could call instance.myExternalMethod() without throwing an error.
  • I then un-commented the function stub, reverting the file to its original state, and everything was still working fine. So essentially the issue fixed itself somehow. All of the other external methods also started working (they had all been causing the same error).
So the issue seems to have fixed itself for me, but I have no idea what caused it in the first place, or why it was fixed. Figured I'd add my experience here in case it helps anyone else or helps figure out the cause of the issue.


回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by