フィルターのクリア

reading part of a very large txt file

4 ビュー (過去 30 日間)
yonatan s
yonatan s 2018 年 2 月 18 日
コメント済み: yonatan s 2018 年 2 月 18 日
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.

採用された回答

Guillaume
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
  1 件のコメント
yonatan s
yonatan s 2018 年 2 月 18 日
thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by