Out of Memory using fgetl

I would like to read each line of a huge text file (.txt). The text file has 3 columns of floating point number like below (about 8 million such lines in text file)
-0.02500000 -0.02450000 -0.02400000
-0.02350000 -0.02300000 -0.02250000
-0.02200000 -0.02150000 -0.02100000
My objective is to read each line at a time. Hence, I thought of using fgetl. But I get an Out of memory error when fgetl is executed for the first time
fid=fopen('allnrncoords.txt','r');
tline=fgetl(fid);
It seems fgetl is trying to read the whole file when it should just read one line. Can someone explain me what could be an issue

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 2 月 3 日
Try using 'rt' instead of 'r' in the fopen()

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

 採用された回答

Sanket
Sanket 2012 年 2 月 6 日

0 投票

The problem was in the text file I was trying to read. The text file was written using %8.8f. When I changed it to %12.8f, fgetl ran fine.

その他の回答 (1 件)

Kevin Holst
Kevin Holst 2012 年 2 月 3 日

0 投票

Just throwing out ideas here, is your error coming at the first call to fgetl or after calling fopen?
Could it be that matlab is not recognizing an eol character in your text file and attempting to get the whole file as a line?

3 件のコメント

Sanket
Sanket 2012 年 2 月 3 日
error is coming at first call to fgetl. fopen works fine.
Yes, its a possibility that matlab is not recognizing eol. Do you have any work around?
Kevin Holst
Kevin Holst 2012 年 2 月 3 日
If you know that each line will always consist of 3 numbers, you could do something like:
fid=fopen('allnrncoords.txt','r');
tline=textscan(fid,'%f %f %f',1);
That will get one line of data, so you could just do that as many times as you needed.
Walter Roberson
Walter Roberson 2012 年 2 月 3 日
Opening with 'rt' should make end of line recognition better. Unless, that is, the file happens to have been written on Mac OS (before OS-X) using only CR without LF as the line breaks.

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

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

タグ

質問済み:

2012 年 2 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by