Reputation: 329
I need to get Matlab R2013a to look in a directory for all files containing the '.txt' extension and then do certain mathematical expressions on those files. Afterwards, the script must print out the data in a file that is labelled with the same name as the input file, except with a few new words added so that I can tell the difference, such as:
Input:
file1.txt
file2.txt
Output:
processed_file1.txt
...etc
I have tried getting matlab to load a directory list and loop the files through the operations that way but I am only getting output for a single file instead of the several hundred in the folder. Thanks for any help.
Upvotes: 2
Views: 6273
Reputation: 849
Not too difficult. Just make a 'processed' folder and save them there. Don't forget the built in shell in MATLAB at your disposal. The most important lines in the following code are the first two and then the filename
line. They make the new folder (line 1), read the .txt contents into a struct called data
(line 2), then the file names are retrieved. Note that you can easily make filename
an array if you need to open and save the files in different loops.
mkdir processed
data = dir('*.txt');
for i = 1:length(data)
filename = data(i).name;
% read data and do your processing
% then save with something like:
fid = fopen(['processed\' filename],'w');
fprintf(...)
fid = fclose(fid);
end
Upvotes: 5