Borrar filtros
Borrar filtros

reading part of a very large txt file

4 visualizaciones (últimos 30 días)
yonatan s
yonatan s el 18 de Feb. de 2018
Comentada: yonatan s el 18 de Feb. de 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.

Respuesta aceptada

Guillaume
Guillaume el 18 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by