How to automatically run all matlab files in a folder

If there are many matlab edits in my folder, is there any command in matlab that can automatically read the edit in the folder and run it?

1 件のコメント

Image Analyst
Image Analyst 2022 年 11 月 13 日
What do you mean? Do you want to locate any m-files that have been edited since some previous time? And then run those m-files or find the lines in the file that are different from some earlier reference version of the file? You know that dir can give you the time date stamp of the files, right?

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

回答 (1 件)

Karim
Karim 2022 年 11 月 12 日

1 投票

something like this should work
folder = 'YourFolder';
mfiles = dir(fullfile(folder, '*.m'));
% loop over the files
for k = 1:length(mfiles)
file = mfiles(k).name;
try
% try to run the current m file
run(fullfile(folder, file));
fprintf('succesful run: %s\n', file);
catch
% print message if it failed
fprintf('failed to run: %s\n', file);
end
end

4 件のコメント

peter huang
peter huang 2022 年 11 月 13 日
great!
But I found that each malab file has a clear all command. If it is not cleared, the program will run incorrectly due to the variable naming. Can you think of a way to avoid it? Because if there is clear all, this group of codes will fail to run
Karim
Karim 2022 年 11 月 13 日
Indeed, if some (or even all) of the routines have a "clear all" then it won't work.
Stephen23
Stephen23 2022 年 11 月 13 日
編集済み: Stephen23 2022 年 11 月 13 日
"Can you think of a way to avoid it?"
By writing more robust code (i.e. functions and not scripts, not calling anti-pattern CLEAR ALL anywhere, etc.)
peter huang
peter huang 2022 年 11 月 14 日
So when I write the code, it should be written as funtion to test the cases of various situations, isn't it using script for testing?

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

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

質問済み:

2022 年 11 月 12 日

コメント済み:

2022 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by