Batch processing of .m files
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Matt J
on 23 Oct 2012
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.
0 Comments
Jan
on 23 Oct 2012
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
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!