Save folder one above current directory

Let's say I am working in the directory:
/home/username/Matlab/
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:
newdir='home/username';
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

 채택된 답변

Jan
Jan 2015년 2월 7일

9 개 추천

Folder = cd;
Folder = fullfile(Folder, '..');
save(fullfile(Folder, 'FileName.mat'))

댓글 수: 11

Zynk
Zynk 2015년 2월 9일
Thanks for your answer. It works, but I prefer the method given by Geoff.
Jan
Jan 2015년 2월 9일
편집: Jan 2015년 2월 9일
The ".."-method works correctly if you are in a root folder like "C:\" already and it considers even UNC-paths like "\\server\folder\". It works under Windows and Linux.
Stephen23
Stephen23 2015년 2월 9일
This is the more universal solution. Use this.
fullfile(cd, '..')
gives only the current directory, and added a file separator and the two dots to it. This correlates with the matlab documentation for fullfile:
"Relative directories indicated by the double-dot symbol are not collapsed."
Tested on Matlab 2017b, Windows 10.
Is there a working solution?
Stephen23
Stephen23 2019년 8월 27일
편집: Stephen23 2021년 7월 1일
"Is there a working solution?"
To what? Relative filenames work perfectly with MATLAB, so to read/write data files it is not neccesary to "collapse" anything, you can simply use '..' by themselves (or append them to pwd, or even better with '.' to specify the current directory). Using a relative path with '..' answers the original question.
If you really want to get the absolute path, just use fileparts on the string returned by pwd.
Ryan Budde
Ryan Budde 2020년 12월 9일
편집: Ryan Budde 2020년 12월 9일
To load a file in Windows 10, I needed the following in order for it to work. Assuming the following folder structure:
ParentFolder>
Subfolder1
Subfolder2
Where Subfolder1 contains the file of interest and Subfolder2 is the current working directory. In this scenario, the following works:
fn = cd;
fn = fullfile(fn, '..');
fn = [fn '\Subfolder1\fileOfInterest.mat'];
data = load(fn);
Cecilia S.
Cecilia S. 2021년 6월 16일
this does not work in new versions of matlab, fullfile() recognises ".." as a folder name and not a "command" to go back one folder, so it creates a path using like this: "Folder/.."
Stephen23
Stephen23 2021년 6월 17일
편집: Stephen23 2021년 6월 17일
@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.
EagleGB
EagleGB 2022년 8월 18일
I'm on a Mac, working in AppDesigner. I am having a similar issue. If I append a '..' to the filename, it works to go up one folder in terminal and in the file directory bar in MATLAB. But when I try to do the same thing in uigetfile, it doesn't go up one directory:
DefOutputDirectory = fullfile(app.outputDirectory,'..');
[NewFile,NewPath] = uigetfile('*.*','Select Destination Folder for Results',DefOutputDirectory);
This just points uigetfile to the directory in app.outputDirectory. Is this a problem with the way uigetfile works?
Stephen23
Stephen23 2022년 8월 19일
편집: Stephen23 2022년 8월 19일
"Is this a problem with the way uigetfile works?"
No, there is no problem with how UIGETFILE works. The UIGETFILE documentation states "To specify a folder name only, make the last character of the defname value either a backslash (\) or a slash (/)."
Once you add the trailing fileseparator it will work as you require and as documented. As an alternative, if you are using FULLFILE it might be easier to add a trailing asterisk like this:
fullfile(..,'..','*')
EagleGB
EagleGB 2022년 9월 2일
Thanks Stephen. Adding the '*' to the end of the fullfile string worked for me.

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

추가 답변 (3개)

Geoff Hayes
Geoff Hayes 2015년 2월 7일

13 개 추천

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!

댓글 수: 2

I would set the 2nd line of answer to:
idcs = strfind(mydir,filesep);
in case running code on Windows. Very useful method, incorporating it into my code right now.
YT
YT 2019년 1월 31일
Agree with @JSPERNY. This method also works with Matlab Online (unlike @Jan's answer).

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

Ivan Nascimento
Ivan Nascimento 2021년 6월 30일

1 개 추천

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

Stephen23
Stephen23 2021년 7월 1일
편집: Stephen23 2021년 7월 1일
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:
fullfile('.','..',nameofmyfile)

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

Walter Adame Gonzalez
Walter Adame Gonzalez 2022년 8월 9일
편집: Walter Adame Gonzalez 2022년 8월 9일

0 개 추천

Try using the following function
syntax:
newDir = getParentDir(dir, numUpDirs);

댓글 수: 1

Stephen23
Stephen23 2022년 8월 9일
편집: Stephen23 2022년 8월 9일
Note that GETPARENTDIR does not work with relative directories, nor with dot directory names:
It also hardcodes a backslash file separator (rather than being OS agnostic by using FILESEP or FILEPARTS).

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

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2015년 2월 6일

댓글:

2022년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by