Which file Matlab selects if two different files with the same name are put under two folders, which are both added via addpath?

조회 수: 50 (최근 30일)
Hi,
I have two functions with the same name. The contents of the functions are different although they share the same type of input and output. I put two functions (.m files) in 2 different folders and I add to Matlab path by calling addpath(genpath(folder_location)).
If I call this function, how does Matlab determine which m file to call? It seems to me that different Matlab versions handle differently. However, the way Matlab does is also not very clear. I am looking for the information for 2015a and 2017a.
Thanks,
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 7월 2일
A far as I recall, R2015a and R2017a were both the same for .m files that are not part of classes and not import'd and not in + directories or 'private' directories. In both cases, the current directory would be searched first, and then the path would be searched in path order. Which is why addpath has specific -BEGIN and -END options, to permit you to tweak the order. (addpath defaults to adding at the beginning of the path.)
John
John 2020년 7월 2일
편집: John 2020년 7월 2일
I just noticed the difference between 2015a and 2017a is this:
If I am add the current folder which as subfolder1 and subfolder2. I call the commands in the following order:
addpath(genpath(subfolder1));
addpath(genpath(subfolder2));
addpath(genpath(subfolder1));
For 2015a, if I open Matlab search path in "Set Path" button, the top path I see after the commands are subfolder1, subfolder2, and subfolder1, respectively
For 2017a, if I open Matlab search path in "Set Path" button, the top path I see after the commands are subfolder1, subfolder2, and subfolder2, respectively. That means 2017a doesn't move up/re-add on top if that path is already in the search path
I think that is causing me problem in 2017a when trying to use 2017a to run a model has been working well in 2015a.

댓글을 달려면 로그인하십시오.

채택된 답변

Monisha Nalluru
Monisha Nalluru 2020년 7월 8일
This is a known issue with addpath function in MATLAB R2017a and R2017b. This got fixed in R2018a. It is recommended to update the MATLAB version.
As a workaround for this issue is to remove the path and then add. This results in adding the path at top.
addpath(genpath('subfolder1'))
addpath(genpath('subfolder2'))
% Perform the required operations
% You can use restoredefaultpath command to set the path to default
rmpath('subfolder1')
addpath('subfolder1') % This will add the path on top again

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 5월 15일
See this documentation page for a discussion of the function precedence order.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by