Auto fetch input files to process from a specified folder
9 ビュー (過去 30 日間)
古いコメントを表示
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);
...
採用された回答
per isakson
2021 年 5 月 17 日
編集済み: per isakson
2021 年 5 月 17 日
"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 件のコメント
per isakson
2021 年 5 月 18 日
編集済み: per isakson
2021 年 5 月 19 日
"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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!