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 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Mathieu NOE
on 28 Oct 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 Comments
Stephen23
on 29 Oct 2020
Rather than using string concatenation, the recommended (and correct tool for the task) is to use fullfile:
fileinFull = fullfile(d1,ailine)
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!