Batch processing of .m files

4 ビュー (過去 30 日間)
Vijay
Vijay 2012 年 10 月 23 日
Hi!
I have a set of .m files with file names in sequence (e.g., 1.m, 2.m,..., 100.m). Each file is having a matrix named X with different values.
Within a common .m file (say BatchPro.m) I wish to call files 1.m, 2.m, .... one by one using a for loop to get the data stored on it (matrix X), so that I can process individual values of the matrix X. How to do this batch process?
I m looking for something like:
S = [];
FileList = dir('*.m'); N = size(FileList,1);
for k = 1:N
FileList(k); %To call 1.m, 2.m, ...
S(k) = mean(X); % X is a row vector in stored in 1.m, 2.m, ...
end

回答 (2 件)

Matt J
Matt J 2012 年 10 月 23 日
You can use EVAL to call the different mfiles, but you should correct whatever got you into the awkward situation of needing to do this.

Jan
Jan 2012 年 10 月 23 日
You cannot start a function or script, when its file is called "1.m". How would the call look like?
1
?! M-file names must be valid Matlab symbols: Starting with a letter, only letters, digits and the underscore, less than 64 characters.
Therefore this will work:
FileList = dir('*.m');
N = size(FileList,1);
S = zeros(1, N); % Pre-allocate!!!
for k = 1:N
File = FileList(k).name;
copyfile(File, 'myTempScript.m');
clear('myTempScript');
run('myTempScript'); % Assuming that the files are scripts
S(k) = mean(X);
delete('myTempScript.m');
end

カテゴリ

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