Auto fetch input files to process from a specified folder

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);
...

 Respuesta aceptada

per isakson
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

Thank you per isakson.
I would also want to save a jpg output file. How to change .txt to .jpg in the output_name?
I also got an error in text read line. File not found.
The values at ffs and ffs_list are right.
per isakson
per isakson el 18 de Mayo de 2021
Editada: per isakson el 18 de Mayo de 2021
"How to change .txt to .jpg in the output_name?" Replace
output_name = insertBefore( ffs, '.txt', 'T_esa' );
by
output_name = insertBefore( ffs, '.txt', 'T_esa' );
output_name = strrep( output_name, '.txt', '.jpg' ); % or the new function, replace()
Not very elegant but it should work.
"error in text read line. File not found" Using dir() and fullfile() should guard against "File not found".
(I assume there isn't a space in the filename, ffs.) I cannot guess the reason (I never use textread()). (A strange character in the name, ffs )
Set Pause on Errors . And when the execution halts run which(ffs) in the command window.
Pradeep Chandran
Pradeep Chandran el 18 de Mayo de 2021
Editada: Pradeep Chandran el 18 de Mayo de 2021
which(ffs) - Unrecognized function or variable 'ffs'.
You wrote "The values at ffs and ffs_list are right."
Add the statement disp(ffs) as the first statement inside the for-loop. And run the the script.
without pause on errors, the command prompt shows following errors. While ffs holds the value "C:\Users\Chandran\Desktop\a\1_mj.txt" (the first file from my directory)
Error using exist
The first input to exist must be a string scalar or character vector.
Error in textread>noargname (line 187)
arg = f(arg);
Error in textread (line 154)
if (noargname(@exist,varargin{1}) ~= 2 || noargname(@exist,fullfile(cd,varargin{1})) ~= 2) ...
Error in Untitled (line 9)
[xdata, ydata] = textread(ffs,'%f %f','headerlines',0);
-------------------------------------------------------------
with the pause on errors selected, K>>which(ffs) displays 'Unrecognized function or variable 'ffs'.
187 arg = f(arg);
in textread.m
-------------------------------------------------------------
I think it is something to do with the syntax of textread.
"I think it is something to do with the syntax of textread." But since which(ffs) throws an error, I'm don't think so.
disp(ffs) shows C:\Users\Chandran\Desktop\a\1_mj.txt - I assume
Little test of textread() (R2018b)
>> textread("i16.txt","%f","headerlines",0)
ans =
1
1
1
The syntax seems to be ok. Which release of Matlab do you use?
I'm convinced it's something weird with ffs. "\Desktop\a\1_mj.txt" Do you have a folder named a on the desktop?
Put a breakpoint at disp(ffs) and step one line at time until textread(). Inspect ffs after each step.
"disp(ffs) shows C:\Users\Chandran\Desktop\a\1_mj.txt - I assume" - that's right. but inside {' '}. I hope that is because it is a cell value.
Which release of Matlab do you use? - R2021a
Do you have a folder named a on the desktop? -Yes
P=uigetdir;
sad=dir(fullfile(P,'*.txt'));
ffs_list=fullfile({sad.folder}, {sad.name});
for ffs=ffs_list
disp(ffs)
[xdata, ydata] = textread(ffs,'%f %f','headerlines',0);
per isakson
per isakson el 18 de Mayo de 2021
Editada: per isakson el 18 de Mayo de 2021
Sorry, mixing Matlab strings and characters confuses me. Now, I have better guess.
ffs is a scalar cell array contaning a character row vector. Replace all ffs inside the for-loop by ffs{1}.
>> ffs = {'i16.txt'}
ffs =
1×1 cell array
{'i16.txt'}
>> which(ffs)
Error using which
Must be a string scalar or character vector.
But I get a different error message (with R2018b).
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 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 17 de Mayo de 2021

Editada:

el 19 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by