Auto fetch input files to process from a specified folder

11 vues (au cours des 30 derniers jours)
Pradeep Chandran
Pradeep Chandran le 17 Mai 2021
Modifié(e) : per isakson le 19 Mai 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);
...

Réponse acceptée

per isakson
per isakson le 17 Mai 2021
Modifié(e) : per isakson le 17 Mai 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 commentaires
Pradeep Chandran
Pradeep Chandran le 18 Mai 2021
Modifié(e) : Pradeep Chandran le 18 Mai 2021
Now this works.
for z=1:length(sad)
ffs=sad(z).name;
[xdata, ydata]...
Now disp(ffs) before textread shows only the filename without the folder path.
Thank you per isakson for your time.
per isakson
per isakson le 18 Mai 2021
Modifié(e) : per isakson le 19 Mai 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by