Problems after upgrading from MATLAB R2013a to MATLAB R2015a - exist gives back '7' for non-existing folder

조회 수: 5 (최근 30일)
Eversince I upgraded to MATLAB R2015a I keep getting problems with my older code. I run the same code on identical data, but I get different results (without getting back errors). In this case I am running a little script in which I'm checking which the latest version folder is available to append to my path:
cd(PathBeforeVersion);
Verfind3 = 'Versionv3.0';
Verfind2 = 'Versionv2.0';
if isequal(exist(Verfind3, 'dir'),7) % 7 = folder.
Verfolder = Verfind3 ;
elseif isequal(exist(Verfind2, 'dir'),7) % 7 = folder.
Verfolder = Verfind2 ;
else
display('no Version folder!');
end
PathAfterVersion = fullfile(PathBeforeVersion,Verfolder);
cd(PathAfterVersion);
So what happens is that even though the 'Versionv3.0' folder doesn't exist ('Versionv2.0' exists), the 'exist' gives back 7, thus making Verfolder = Verfind3 and then the path is wrong and cannot cd. I ran for non-existent 'Versionv4.0' and correctly got back a zero from exist. I literally have the folder of the current dir before my eyes, type 'dir' and no such folder exists. Does anyone have any idea what could be happening? Could it be that the 'exist' doesn't just check in my current directory? Is there some setting I could have wrong? (This code worked perfectly on MATLAB R2013a).
Apologies if it is something obvious, but I have run out of ideas.
Thanks in advance.

채택된 답변

Jan
Jan 2015년 12월 14일
Avoid using realtive paths, because a timer or GUI callback can change the current folder unexpectedly. exist searches the complete Matlab path, such that it is prone to bugs. Better:
% Omit: cd(PathBeforeVersion);
Verfind3 = 'Versionv3.0';
Verfind2 = 'Versionv2.0';
if isequal(exist(fullfile(PathBeforeVersion, Verfind3), 'dir'), 7)
Verfolder = Verfind3 ;
elseif isequal(exist(fullfile(PathBeforeVersion, Verfind2), 'dir'), 7)
Verfolder = Verfind2 ;
else
% Better stop with an error! display('no Version folder!');
error('Author:Function:unfoundFolder', 'Cannot find folder in: %s', ...
PathBeforeVersion);
end
  댓글 수: 1
ziggy
ziggy 2015년 12월 14일
Thank you very much, Jan, this was really helpful. I will definitely avoid the relative paths from now on.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 14일
편집: Walter Roberson 2015년 12월 14일
exist() does search the search path for folders; or at least it does so in R2014a.
Remember, you can ask about ./Versionv3.0 if you want to look only in the current folder.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by