Importing a single piece of data from a lot of txt files
Show older comments
I usually feel pretty comfortable with matlab but this one has me scratching my head.
I have about 500 text files. Each with a [4500x5] set of data. I need to get one point of data from each file like [2350,3] and put that in an array with that data point from all 500 text files.
I am having difficulty getting this to work without having to import each of the 500 text files and all of the data points.
I have tried combining all the text files into one and importing it and using a for loop to get the data point that I need and then iterating to the next one in the list. The problem here was that when combined all of the text files have over 10 million points of data. When I started the loop it crashed my computer.
Is there an easier way to gather this one point of data from each one of the 500 text files?
Answers (1)
Walter Roberson
on 30 Sep 2011
cols_per_file = 5;
wantrow = 2350;
wantcol = 3;
linepattern = [repmat('%*f',1,wantcol-1), '%f', repmat('%f',1,cols_per_file - wantcol)];
indata = zeros(numfiles,1);
for K = 1 : numfiles
thisfile = filenames{K};
fid = fopen(thisfile, 'rt');
onevalue = textscan(fid, linepattern, 1, 'HeaderLines', wantrow-1);
fclose(fid);
indata(K) = onevalue{1};
end
You might want to dress this up a bit with try/catch just in case a file is not readable or somehow comes out too small.
3 Comments
Derek
on 30 Sep 2011
Derek
on 30 Sep 2011
Walter Roberson
on 30 Sep 2011
My code presumes that you have already created a cell array of strings that contains the names of the files to work with.
For example,
filenames = cellstr(num2str((1:numfiles).', 'Final AVG PIC%03d.jvc'));
Or if appropriate you could use dir() to scan the directory and find all of the 'Final AVG PIC*.jvc' files, and then assign numfiles as the number of entries returned by dir():
fileinfo = dir('Final AVG PIC*.jvc');
filenames = {fileinfo.name};
numfiles = length(filenames);
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!