필터 지우기
필터 지우기

problem with reading whole line with fgets and fgetl

조회 수: 14 (최근 30일)
Ali
Ali 2011년 5월 24일
답변: Lynn Neergaard 2020년 9월 30일
I am tryiong to read a data file which a number of lines of headers as well as column headings. Right now I cannot get a full line read by Matlab.
I can load the file and I can read a line into a string, but it gives me only a limited number of characters, but not always the same number of characters. Both fgets and fgetl give me this problem!
I should add maybe that I am working with R2007a.
Thanks!
  댓글 수: 4
Ali
Ali 2011년 5월 24일
Sorry for the previous post but I have seen what is wrong now. The array editor window shows only part of the line that it has read even if the cell holding the data is increased. When I hover over the cell I can see that it has in fact copied all the data.
Thanks for attempting to help!
Lynn Neergaard
Lynn Neergaard 2020년 9월 30일
편집: Lynn Neergaard 2020년 9월 30일
I have a problem that matches this description exactly, but is not solved by noticing the remainder of the read line.
I am running Matlab 2019b on a Windows 10 machine. My code is as follows
------
FileName = 'Test.out.txt'
fid = fopen(FileName);
Tdata = [];
while isempty(Tdata)% || Parens ~= 0% Skip header lines
% Conflabit. The following line of code only reads
% the first half of the line of data. fgets same
% deal.
lineA = fgetl(fid);
Tdata = sscanf(lineA, '%f');
end
fclose(fid)
The line I want to read is
1 0.000 286 238.82 238.82 238.82 0.0000 5.373E+04 269.05 2.705E+05 7.003E-01 7.040E+02 0.000E+00 1.894E+05 0.000
but what I get stops at the set of 5 spaces in the middle, that is
lineA = '1 0.000 286 238.82 238.82 238.82'
There is a lot more code and a lot more data, but there are limitations of data rights, so I had to extract a portion of the code and the data. Turns out, the files I attached work just fine. I will see if I can reproduce the error with a sanitized version of both code and data. Please stand by.

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2011년 5월 24일
fgets() or fgetl() are usable up to a minimum of 2 gigabytes per line (larger on 64 bit MATLAB.) Of course, they might run out of memory before that point.
Are you opening the file with 'r' (a binary file) or with 'rt' (a text file)? Which operating system are you using? Are you specifying a file encoding when you open the file?
Is it possible that the file is open and being written to by another program?

Ali
Ali 2011년 5월 24일
The array editor window shows only part of the line that it has read even if the cell holding the data is increased. When I hover over the cell I can see that it has in fact copied all the data.
Hopefully this helps other people not make the same dumb mistake!

Lynn Neergaard
Lynn Neergaard 2020년 9월 30일
OK. I figured my problem out. I was reading a COMPLETELY DIFFERENT file than the one I thought I was reading. The file I actually had open (you know, the fopen and all that) at the point in my code had a subset of the data of the file I thought I had open.
In conclusion, fgetl was reading the entire line, just it was a line from a different file. For the reader's edification, it could have been a line in the same file. There just are not a lot of indicators you are using the fgetl command where you think you are, even in troubleshooting mode.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by