Problems reading a textfile; fgetl reads *****

Hello,
I am trying to read text files of this format
1 1 1951 5.8
2 1 1951 0.0
3 1 1951 3.4
4 1 1951 0.0
5 1 1951 0.0
6 1 1951 0.0
... (until 2099, there are 54422 lines in each file).
Reading the files works well until for example
23 2 1997 171.7
which is read as
23 2 1997 *****
if I use fgetl, or if I continue reading the file with fscanf (e.g.
A=fscanf(fid,'%d %d %d %f',4)
nothing is read in for A(4).
If I open the file in Notepad++ it looks normal, the numbers are seperated by 1 to 3 blanks, the file has Unix line endings. Opening the file in Notepad++ and saving it solves the problem, but I have 294000 files, so that's not an option. If I skip that particular line, there is also no problem reading the other lines of the file. The problem only occurs every few thousand files.
This seems very weired. Does anybody have an idea, what the problem could be?
Thanks a lot, Doris

 Respuesta aceptada

Jan
Jan el 18 de Nov. de 2011
Thanks for sharing the file. I really find
23 2 1997 *****
in the specific line. No encoding problem, no text-mode tricks. There are really stars - ASCII(42). Opening it in NotePad++ and saving it does not change anything.
So I'm wondering how do you know, that it should be
23 2 1997 171.7
? Did you write this is FORTRAN with "FORMAT(F5.1)"?

1 comentario

Doris
Doris el 19 de Nov. de 2011
Sorry to all for my stupid question, and shame on me. I mixed up the digits in the filename and always looked at the wrong file in the editor.
Thank you, Doris

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 18 de Nov. de 2011

0 votos

Please show us how you open the file. Is it a problem of a Unicode conversion? Then read the line using fgetl and convert the values by double - anything above 255?!
How do you exactly determine that "23 2 1997 ***" is read? This is not trivial, because FSCANF does not reply numbers, if the reply has different types.
A really strange problem. But I've never seen Matlab acting magically. Therefore I'm convinced that it is a problem in the code.

3 comentarios

Doris
Doris el 18 de Nov. de 2011
I open the file simply with "fid=fopen(filename,'r');"
"Then read the line using fgetl and convert the values by double - anything above 255?!"
I am not sure whether I understood this correctly. If I read the line with "test=fgetl(fid)" test =23 2 1997 *****
If I then write "double(test)" the reply is ans =
50 51 32 32 50 32 49 57 57 55 32 42 42 42 42 42
I just assumed that the line was read. How should I find out?
Thanks,
Doris
Jan
Jan el 18 de Nov. de 2011
@Doris: Wow. Strange.
Walter Roberson
Walter Roberson el 18 de Nov. de 2011
Try opening the file with 'rt' instead of 'r'

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Nov. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by