Auto fetch input files to process from a specified folder
8 views (last 30 days)
Show older comments
Pradeep Chandran
on 17 May 2021
Edited: per isakson
on 19 May 2021
I have a lot of data files that I would like to process through my code, which will generate output files. Currently I have written a program wherein I have to type each file name in the code and generate the output file. And I have to repeat it for every data file. Is there any way to program so as to take each file from a specified folder one at a time and generate the output file in the same folder, till all the files in the specified folder is exhausted. The output file name shall be "input-filename"+T_esa.txt. i.e. if the datafile is 18_mj.txt then the output filename should be 18_mjT_esa.txt.
The data file will contain characters, numerals, and special characters.
Extracts from the current code is given below.
If you need anyother information kindly comment below.
Any help would be appreciated. Thank you for your time.
...
[xdata, ydata] = textread('C:\Users\dell\Desktop\18_mj.txt','%f %f','headerlines',0);
...
plot(xdata,T,'r')
exportgraphics(gca,'C:\Users\dell\Desktop\18_mjT_esa.jpg')
...
fid = fopen('C:\Users\dell\Desktop\18_mjT_esa.txt','w');
fprintf(fid,'%3.5f \n',T);
fclose(fid);
...
Accepted Answer
per isakson
on 17 May 2021
Edited: per isakson
on 17 May 2021
"I have a lot of data files that I would like to process through my code [...] each file from a specified folder one at a time"
IMO: The best approach
- store the data files of interest in a specific folder (and its subfolders)
- include some shared characteristic in the names of the data files, e.g. "mj.txt"
sad = dir( fullfile( folder_name, '*mj.txt') );
ffs_list = fullfile( {sad.folder}, {sad.name} ); % full file specifier
for ffs = ffs_list
% read one data file, e.g.
[xdata, ydata] = textread( ffs,'%f %f','headerlines',0);
% do something with the data
output_name = insertBefore( ffs, '.txt', 'T_esa' );
fid = fopen(output_name,'w');
% und so weiter
end
P.S. The format specifier, '%f %f', doesn't match "The data file will contain characters, numerals, and special characters."
11 Comments
per isakson
on 18 May 2021
Edited: per isakson
on 19 May 2021
"Replacing all ffs inside the for-loop by ffs{1}" would solve it. And that gives a more robust code, which doesn't depend on the working directory or Matlab search path.
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB 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!