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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by