フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

how to write change directories and run the m file in related directory from the the top level folder?

2 ビュー (過去 30 日間)
Homayoon
Homayoon 2015 年 8 月 31 日
閉鎖済み: Stephen23 2015 年 8 月 31 日
Dear All,
I am still trying to figure how to do with my problem. Still struggling without any significant Improvement. Well I decide to rephrase my question in order to make it more understandable.
I have a folder which name is Main. in this folder I have 10 subfolders whose names are 1 through 10.in each subfolder there is only one m file. this m file performs calculations on the text files in each subfolder.
I need to have a code located in my main folder which iteratively changes the path to each subfoledr and run the m file located in that subfolder. Of course the output files of the m file should be dumped out in the same subfolder as the m file exists.
This will save much time of me. Thanks in advance.
Best
HRJ
  3 件のコメント
Homayoon
Homayoon 2015 年 8 月 31 日
The answer written there, does not run the m file.
Walter Roberson
Walter Roberson 2015 年 8 月 31 日
That is not a reason to create a new Question. If someone's answer does not meet your needs then clarify your needs without writing a new Question.

回答 (2 件)

Homayoon
Homayoon 2015 年 8 月 31 日
for f=1:10;
somefolder=sprintf('%s%d%s','C:/Users/Desktop/New folder/00', f, '/');
cd (somefolder);
run H2Evolution.m
end

Walter Roberson
Walter Roberson 2015 年 8 月 31 日
projectDir = 'Main';
maxdir = 10;
if ~exist(projectDir, 'dir')
error('top level directory not found');
end
startdir = cd();
for FolderNumber = 1 : maxdir
cd(projectDir);
foldername = sprintf('%d', FolderNumber);
if ~exist(foldername, 'dir')
warning(sprintf('Subdirectory not found: %s', foldername));
continue
end
cd(foldername);
WhateverTheOneMFileNameIsGoesHere;
end
cd(startdir);
  3 件のコメント
Homayoon
Homayoon 2015 年 8 月 31 日
however, it is fixed once i replace projdir by the directory path itself
Walter Roberson
Walter Roberson 2015 年 8 月 31 日
startdir = cd();
projectDirName = 'Main';
projectDir = fullfile(startdir projectDirName);
maxdir = 10;
if ~exist(projectDir, 'dir')
error('top level directory not found');
end
for FolderNumber = 1 : maxdir
cd(projectDir);
foldername = sprintf('%d', FolderNumber);
if ~exist(foldername, 'dir')
warning(sprintf('Subdirectory not found: %s', foldername));
continue
end
cd(foldername);
H2Evolution;
end
cd(startdir);

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by