Filter löschen
Filter löschen

Save folder one above current directory

562 Ansichten (letzte 30 Tage)
Zynk
Zynk am 6 Feb. 2015
Kommentiert: EagleGB am 2 Sep. 2022
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

Akzeptierte Antwort

Jan
Jan am 7 Feb. 2015
Folder = cd;
Folder = fullfile(Folder, '..');
save(fullfile(Folder, 'FileName.mat'))
  11 Kommentare
Stephen23
Stephen23 am 19 Aug. 2022
Bearbeitet: Stephen23 am 19 Aug. 2022
"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 am 2 Sep. 2022
Thanks Stephen. Adding the '*' to the end of the fullfile string worked for me.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Geoff Hayes
Geoff Hayes am 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!
  2 Kommentare
JSPERNY
JSPERNY am 19 Mär. 2018
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 am 31 Jan. 2019
Agree with @JSPERNY. This method also works with Matlab Online (unlike @Jan's answer).

Melden Sie sich an, um zu kommentieren.


Ivan Nascimento
Ivan Nascimento am 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 Kommentar
Stephen23
Stephen23 am 1 Jul. 2021
Bearbeitet: Stephen23 am 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:
fullfile('.','..',nameofmyfile)

Melden Sie sich an, um zu kommentieren.


Walter Adame Gonzalez
Walter Adame Gonzalez am 9 Aug. 2022
Bearbeitet: Walter Adame Gonzalez am 9 Aug. 2022
Try using the following function
syntax:
newDir = getParentDir(dir, numUpDirs);
  1 Kommentar
Stephen23
Stephen23 am 9 Aug. 2022
Bearbeitet: Stephen23 am 9 Aug. 2022
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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by