Set variable equal to folder in path
Show older comments
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.
Answers (2)
Steven Lord
on 4 May 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 Comment
Andrew Biehl
on 4 May 2020
Andrew Biehl
on 7 May 2020
0 votes
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!