reading part of a very large txt file

6 vues (au cours des 30 derniers jours)
yonatan s
yonatan s le 18 Fév 2018
Commenté : yonatan s le 18 Fév 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.

Réponse acceptée

Guillaume
Guillaume le 18 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by