Run the same script for multiple folders

3 ビュー (過去 30 日間)
Harry Smith
Harry Smith 2021 年 5 月 12 日
コメント済み: Harry Smith 2021 年 5 月 12 日
Hello,
I have created a script that reads files inside a folder and performs various analyzes.
Each time, I have to go change the directory path and run the script again.
I would like to know if it was possible (perhaps through a for loop of the entire script?) To have the script run for all the various folders in a directory.

回答 (1 件)

Jan
Jan 2021 年 5 月 12 日
編集済み: Jan 2021 年 5 月 12 日
FolderList = dir('C:\Your\BaseFolder\'):
FolderList = FolderList([FolderList.isdir]);
FolderList(strncmp({FolderList.name}, '.')) = []; % Remove . and ..
for k = 1:numel(FolderList)
Folder = fullfile(FolderList(k).folder, FolderList(k).name);
% Now use the Folder to access the files.
end
Prefer to access the files using their absolute path name instead of cd(Folder). Changing the current folder by cd() is fragile, because any callback of a GUI or TIMER might change the folder again during your code runs. Absoulte file names are bullet proof (except if you shoot the bullet into the running hard disk).
  1 件のコメント
Harry Smith
Harry Smith 2021 年 5 月 12 日
sorry i'm self-taught, where should i insert my script in your code?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by