reading a file name from a list of files in a text file line by line and open the files one at a time
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
richg
el 28 de Oct. de 2020
Comentada: richg
el 29 de Oct. de 2020
I have a list of files in a summary text file. Each line is a name with no spaces. I want to read the files in, one at a time, open one, get input from that file, then move on to the next. I have tried fgetl, fgets, and textscan. fgetl returns the correct file name, but fed into an fopen file, it will no open the file. fgets returns the name with a carraige return (not what I want) textscan returns all of the summary text file names as a single input. All of the files exist in the same directory as the summary textfile. Examples are
Sumary test file:
inputfile1
inputfile2
inputfile50
Each file has the same number of ascii text lines.
fid1=fopen('Summary test file')
for 1;1:3
aline=fgetl(fid1)
aline % prints out correct name as text with no spaces)
fid2=fopen(aline) % this wont open, gives fid= -1
end
How do I do this?
8 comentarios
Respuesta aceptada
Mathieu NOE
el 28 de Oct. de 2020
hi
you can try this - I tested it on dummys file. just found that my matlab is pretty slow for such simple tasks....
% read files listed in filesin.txt file
fileID = fopen('filesin.txt');
C = textscan(fileID,'%s');
fclose(fileID);
celldisp(C)
numfiles = numel(C{1});
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = char(C{1}{k});
mydata{k} = importdata(myfilename);
end
celldisp(mydata)
6 comentarios
Stephen23
el 29 de Oct. de 2020
Rather than using string concatenation, the recommended (and correct tool for the task) is to use fullfile:
fileinFull = fullfile(d1,ailine)
Más respuestas (0)
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!