MATLAB Answers

Save folder one above current directory

813 views (last 30 days)
Zynk on 6 Feb 2015
Edited: Stephen on 1 Jul 2021
Let's say I am working in the directory:
I want to save (in a script) the directory one above the one I am working in (to use it later in the script), so:
But I am working in different computers, so the 'username' changes, thus I want to do it automatically, such as save "newdir" as "one above the current directory".
Thank you

Accepted Answer

Jan on 7 Feb 2015
Folder = cd;
Folder = fullfile(Folder, '..');
save(fullfile(Folder, 'FileName.mat'))
Stephen on 17 Jun 2021
@Cecilia S: that is the intended behavior of FULLFILE, exactly as it is documented. It is the OS that assigns meaning to '..', once the path is actually supplied to the OS (e.g. to read/write a file), not FULLFILE.
This is exactly the same as if you were to hard-write a path using '..' somewhere in the folder heirarchy.
So your comment is incorrect: FULLFILE still works correctly, even for "new versions" of MATLAB. You just don't understand how path resolution works, but luckily the people who wrote FULLFILE do:
What you ask for is anyway impossible: for example, only the OS can resolve this path:
FULLFILE does not (and never has, probably never will) actually check if a path exists or is valid on a particular location, nor would this be a useful design (slow, device dependent, superfluous, meaningless if a path is yet to be created). Only the OS can do this, which is why the OS resolves both the dot and dotdot folders:
Using dot and dotdot is the designed, best method to refer to the current and parent (of any) directories.

Sign in to comment.

More Answers (2)

Geoff Hayes
Geoff Hayes on 7 Feb 2015
Zynk - try using pwd with strfind to determine the index of the last occurrence of the slash /. Then just remove it and everything that follows. Something like
mydir = pwd;
idcs = strfind(mydir,'/');
newdir = mydir(1:idcs(end)-1);
Try the above (or a variation of it) and see what happens!
YT on 31 Jan 2019
Agree with @JSPERNY. This method also works with Matlab Online (unlike @Jan's answer).

Sign in to comment.

Ivan Nascimento
Ivan Nascimento on 30 Jun 2021
An alternative solution, as pointed out by Stephen in the comments, is to use fileparts with cd (or pwd) to get the directory above the current. It might be useful depending on the context, as it was for me (R2016a) and seems to be for Cecilia, so here it is.
parentDirectory = fileparts(cd); % also works with pwd instead of cd
If you intend to use the saved string in a different platform, make sure the path delimiters are consistent with the platform. Specifically, you will have trouble from Windows to Unix or Mac systems because on Windows fileparts adopts backslashes by default. The other way around is fine because on Windows you can use either forward (/) or back (\) slashes as path delimiters, according to the fileparts documentation.
  1 Comment
Stephen on 1 Jul 2021
The simple, efficient, robust solution is to just use '..' to refer to the parent directory.
"If you intend to use the saved string in a different platform..."
.. .then you can trivially avoid all of that worrying and messing about with path delimiters by using '.' to refer to the current directory, and '..' to its parent. This will work correctly for MATLAB R2016a (and every other version of MATLAB too) on any platform:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by