MATLAB: Access parent folder when using a directory junction

cddirectoryloadMATLAB

Normally you can switch to or load files from the parent folder by using the double period. This doesn't work when both the parent folder and current folder are in an external drive linked to with a directory junction.
I ran out of space so I moved my project to an external drive. This broke all my links so I made a directory junction to make it look like my project is still in the same folder it was previously in. When I am in a subfolder of my project I can no longer access the parent folder.
  • I am in folder: F:\project_folder\sub_folder1\sub_folder2
  • There is a directory junction so Matlab thinks it is in: C:\Users\UserName\Documents\project_folder\sub_folder1\sub_folder2
  • The junction is at a higher level so C:\Users\UserName\Documents\project_folder -> F:\project_folder
  • I want to cd to sub_folder1 which, of course, is also under the junction
in 2017b you get:
>> cd('..\')
Error using cd
Cannot CD to ..\ (Name is nonexistent or not a directory).
So whats the best alternative way to get the parent folder without the absolute path?

Best Answer

There is a directory junction so Matlab thinks it is in: C:\Users\UserName\Documents\project_folder\sub_folder1\sub_folder2
No. If you pwd, you'll see that matlab think it is in F:\project_folder\sub_folder1\sub_folder2. It seems it's a limitation of matlab. If you cd into a junction matlab actually switch to the target directory instead.
I doubt there's a way around that, you can report that as a bug to mathworks and see what they say. However, Stephen is right, you shouldn't be using cd, which can lead to all sort of bugs by bringing functions in and out of scope. Much better would be to build the path of the files/folders you want to access and use absolute or relative path. E.g. instead of something like
cd somepath
data = load('somefile.mat');
use
data = load(fullfile(somepath, 'somefile.mat'));
which would work properly with junctions.