Azzera filtri
Azzera filtri

reading part of a very large txt file

4 visualizzazioni (ultimi 30 giorni)
yonatan s
yonatan s il 18 Feb 2018
Commentato: yonatan s il 18 Feb 2018
hi,
I have a text file looking like this:
x=1 y=2
1
2
3
4
*
x=4 y=2
4
2
*
x=3 y=1
2
44
*
and so on.
the data I need to read is, for example, all the numbers under x=4 y=2. the text file is very big (over 20 GB) and of course i don't want to read all of it.

Risposta accettata

Guillaume
Guillaume il 18 Feb 2018
Then read the file line by line until you find your pattern, then read the section you need until the next block of xy. Something like:
desiredxy = [4 2];
searchpattern = fprintf('x=%d y=%d', desiredxy);
fid = fopen('c:\somewhere\somefile.txt', 'rt');
assert(fid > 0, 'failed to open file');
datacolumn = [];
insection = false;
while true
line = fgetl(fid);
if ~ischar(line)
error('reached end of file before pattern was found');
end
if insection
%in the section of interest
if line(1) == 'x'
%beginning of next section. Done reading. Quit while loop
break;
else
%read number
datacolumn = [datacolumn; str2double(line)]; %#ok<AGROW>
end
elseif strcmp(line, searchpattern)
%not in the section of interest but just found its start
insection = true;
end
end

Più risposte (0)

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by