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!