is there a code to open and run all m files in a folder?

31 ビュー (過去 30 日間)
Itzik Ben Shabat
Itzik Ben Shabat 2014 年 3 月 22 日
コメント済み: Image Analyst 2014 年 3 月 25 日
Hi, I have multiple (about 50) different .m files in a folder. I wish to open and run each and every one of them using a matlab script. the output of this script would be a logical vector indicating which files had errors and which did not.
so is there a matlab code/ function that can open, run and check if there were run errors? thanks

採用された回答

Jan
Jan 2014 年 3 月 22 日
What about this:
folder = 'C:\YourFolder';
list = dir(fullfile(folder, '*.m');
nFile = length(list);
success = false(1, nFile);
for k = 1:nFile
file = list(k).name;
try
run(fullfile(folder, file));
success(k) = true;
catch
fprintf('failed: %s\n', file);
end
end
  3 件のコメント
Jos (10584)
Jos (10584) 2014 年 3 月 25 日
You can check if the mfile is the current file using MFILENAME, as in,
if ~strcmpi(fullfile(folder, file), mfilename('fullpath'))
...
Image Analyst
Image Analyst 2014 年 3 月 25 日
I don't know what "the figure code" is. Do you mean if the m-file has an associated .fig file? Or do you mean if the word "figure" appears inside the m-file, especially as a statement/function call? Anyway, to see if a certain string appears in another string, you can use strfind().

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

その他の回答 (4 件)

Jakob Sørensen
Jakob Sørensen 2014 年 3 月 22 日
I don't know if there is a built-in function, but I think you could fairly easy make one yourself. Using the command dir you can acquire all the file names in a folder, then just loop through them, running them one-by-one.

Andy Campbell
Andy Campbell 2014 年 3 月 24 日
編集済み: Andy Campbell 2014 年 3 月 24 日
To expand on per's answer, in R2014a you can do this using parameterized tests:
classdef TestAllFiles < matlab.unittest.TestCase
properties(TestParameter)
file = getListOfFiles;
end
methods(Test)
function testFileDoesNotError(testCase, file)
run(fullfile('C:\YourFolder', file));
end
end
end
function files = getListOfFiles
list = dir(fullfile('C:\YourFolder', '*.m'));
files = {list.name};
end
Then you can run them all and get your results:
result = runtests('TestAllFiles')
logicalVector = [result.Passed];
  1 件のコメント
Itzik Ben Shabat
Itzik Ben Shabat 2014 年 3 月 25 日
thank you. i already implemented what Jan offered but ill look into this as well.

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


per isakson
per isakson 2014 年 3 月 22 日
編集済み: per isakson 2014 年 3 月 22 日

Image Analyst
Image Analyst 2014 年 3 月 22 日

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by