Set variable equal to folder in path

8 次查看(过去 30 天)
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.

回答(2 个)

Steven Lord
Steven Lord 2020-5-4
Assuming you start off in MainDir:
S = 'SubDir1';
A = fullfile(pwd, S, 'SubDir2', 'AnalysisDir1');
fprintf('Changing directory to %s.\n', A)
cd(A)
  1 个评论
Andrew Biehl
Andrew Biehl 2020-5-4
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

请先登录,再进行评论。


Andrew Biehl
Andrew Biehl 2020-5-7
Are there any other thoughts on this question? Thanks!

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by