Path not searching in current directory first

On my path I have a folder called "Other_Code" which contains "@myClass/myClass.m". In the directory I am running from I have a file called "myClass.m" which overloads the one in the other folder. Since I am running the code from the same directory, MATLAB should search for the file in my current location first before moving onto the folders in my path. I am getting errors because the search path is somehow bypassing my current directory and accessing the folders in the path first. What's even more strange is that if I put a debug at the point that the function is called, I can use the command "which myClass", it returns the correct location of "currentDirectory/myClass.m". When I step through the code and get an error, the error info links me to the file "Other_code/@myClass/myClass.m".
Why is this happening, and how can I fix it?

 채택된 답변

TARUN
TARUN 2025년 6월 14일
편집: TARUN 2025년 6월 14일

1 개 추천

The issue you're facing stems from MATLAB's class resolution rules, which differ from normal function resolution. Even though which myClass shows your current folder’s version, MATLAB gives priority to the @myClass folder inside your path (Other_Code/@myClass) because class folders on the path override regular .m files in the current folder.
Feel free to try one of the following workarounds to resolve this:
  • Temporarily remove the interfering path using:
rmpath('path_to/Other_Code');
  • Rename your local file (myClass.m) to avoid name conflicts.
  • Use a local @myClass folder in the current directory to ensure precedence.
  • Force MATLAB to refresh the path cache and class definitions:
clear classes;
rehash path;
For more details on how MATLAB prioritizes class definitions, check this documentation:

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2020년 11월 19일

편집:

2025년 6월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by