problem with reading whole line with fgets and fgetl
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Lynn Neergaard
el 30 de Sept. de 2020
Editada: Lynn Neergaard
el 30 de Sept. de 2020
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.
Respuestas (3)
Walter Roberson
el 24 de Mayo de 2011
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?
0 comentarios
Lynn Neergaard
el 30 de Sept. de 2020
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.
0 comentarios
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!