reading part of a very large txt file
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Guillaume
2018-2-18
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!