reading a file name from a list of files in a text file line by line and open the files one at a time

18 views (last 30 days)
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
richg
richg on 28 Oct 2020
here is the dir list
10/28/2020 01:51 AM <DIR> .
10/28/2020 01:51 AM <DIR> ..
10/28/2020 01:51 AM 0 dir_list
10/27/2020 10:12 PM 52,544 dumpH_0.back
10/26/2020 11:24 PM 52,544 dumpH_0gpa_100K.10100.custom
10/26/2020 11:24 PM 20,910 dumpH_0gpa_100K.10100.custom.gz
10/26/2020 11:24 PM 52,564 dumpH_0gpa_100K.10120.custom
10/26/2020 11:24 PM 52,574 dumpH_0gpa_100K.10140.custom
10/26/2020 11:24 PM 52,528 dumpH_0gpa_100K.10160.custom
10/26/2020 11:24 PM 52,531 dumpH_0gpa_100K.10180.custom
10/27/2020 10:15 PM 20,910 dumpH_0gpa_100K.custom.zip
10/27/2020 10:18 PM 52,544 dump_0gpa_100K.10100.custom.txt.txt
10/27/2020 07:37 PM 145 filesin
10/27/2020 10:19 PM 145 filesin.txt
10/27/2020 07:09 PM 95 test_text1
13 File(s) 410,034 bytes
2 Dir(s) 1,346,096,627,712 bytes free
and here is the filesin file:
dumpH_0gpa_100K.10100.custom
dumpH_0gpa_100K.10120.custom
dumpH_0gpa_100K.10140.custom
dumpH_0gpa_100K.10160.custom
dumpH_0gpa_100K.10180.custom
richg
richg on 28 Oct 2020
The hidden windoew extension option was on. That is why there are two .txt endings. I turned it off. Thanks.

Sign in to comment.

Accepted Answer

Mathieu NOE
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
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)
richg
richg on 29 Oct 2020
Thanks - I see it. It allows me to move form Windows to LINUX and back. I do that a lot while writing and debugging.

Sign in to comment.

More Answers (0)

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!