Auto fetch input files to process from a specified folder
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pradeep Chandran
el 17 de Mayo de 2021
Editada: per isakson
el 19 de Mayo de 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);
...
1 comentario
Respuesta aceptada
per isakson
el 17 de Mayo de 2021
Editada: per isakson
el 17 de Mayo de 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 comentarios
Pradeep Chandran
el 18 de Mayo de 2021
Editada: Pradeep Chandran
el 18 de Mayo de 2021
per isakson
el 18 de Mayo de 2021
Editada: per isakson
el 19 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!