Problems after upgrading from MATLAB R2013a to MATLAB R2015a - exist gives back '7' for non-existing folder
    5 views (last 30 days)
  
       Show older comments
    
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.
0 Comments
Accepted Answer
  Jan
      
      
 on 14 Dec 2015
        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
More Answers (1)
  Walter Roberson
      
      
 on 14 Dec 2015
        
      Edited: Walter Roberson
      
      
 on 14 Dec 2015
  
      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.
See Also
Categories
				Find more on Search Path in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

