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

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.

 Accepted Answer

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 Comment

Thank you very much, Jan, this was really helpful. I will definitely avoid the relative paths from now on.

Sign in to comment.

More Answers (1)

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.

1 Comment

Thank you Walter, the './Versionv3.0' might be useful for future searches.

Sign in to comment.

Categories

Asked:

on 14 Dec 2015

Edited:

on 14 Dec 2015

Community Treasure Hunt

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

Start Hunting!