Set variable equal to folder in path

7 visualizzazioni (ultimi 30 giorni)
Andrew Biehl
Andrew Biehl il 4 Mag 2020
Risposto: Andrew Biehl il 7 Mag 2020
I am running a script in multiple directories. They are organized like this:
MainDir/SubDir1/SubDir2/AnalysisDir1
I want to store the string "SubDir1" as a variable, which I will later insert into the name of the output files.
Here's how I accomplish this in cygwin:
cd ./../../
SubDir1="${PWD##*/}"
cd ./SubDir2/AnalysisDir
Any help would be much appreciated.

Risposte (2)

Steven Lord
Steven Lord il 4 Mag 2020
Assuming you start off in MainDir:
S = 'SubDir1';
A = fullfile(pwd, S, 'SubDir2', 'AnalysisDir1');
fprintf('Changing directory to %s.\n', A)
cd(A)
  1 Commento
Andrew Biehl
Andrew Biehl il 4 Mag 2020
Thanks for the code. I don’t think my question was clear. I don’t want to manually assign ‘Subdir’. I want the code to extract ‘Subdir’ from the existing path. Here is my list of Directories. Note that I changed “Subdir1” to “Subdir1a,b,c…”
MainDir/SubDir1a/SubDir2/AnalysisDir1
MainDir/SubDir1b/SubDir2/AnalysisDir2
MainDir/SubDir1c/SubDir2/AnalysisDir3 …
I will run the matlab file using a .bat file which I will use to run multiple different programs in series. For the Matlab portion of the .bat file, I will first change the working directory to the analysis directory (e.g. AnalysisDir1), and then I will run MatlabScript.m within the analysis directory.
I want to have lines of code within MatlabScript.m which will pull SubDir1a (or b,c, etc) out of the path, so that my output can be sent to SubDir1a.txt

Accedi per commentare.


Andrew Biehl
Andrew Biehl il 7 Mag 2020
Are there any other thoughts on this question? Thanks!

Categorie

Scopri di più su Search Path in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by