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 (최근 30일)
이전 댓글 표시
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?
채택된 답변
Mathieu NOE
2020년 10월 28일
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
Stephen23
2020년 10월 29일
Rather than using string concatenation, the recommended (and correct tool for the task) is to use fullfile:
fileinFull = fullfile(d1,ailine)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!