Reading columns from a poorly-formatted text file
Mostrar comentarios más antiguos
I need to read data from an old text file which has fixed columns for the data. I have tried using readtable, which was partly succesfull but seems to have been fooled by the data in places.
A shortened version of the file is attached. It has the following 'features' :
- All columns are not always there
- Sometimes there is no space between the columns
- It uses -998 or -998.0 for NaN;
- There are blank lines.
Is there an easy way to read this file, or do I need to be old-fashioned and read it line-by-line and then parse for the values?
Respuesta aceptada
Más respuestas (1)
Steven Lord
el 29 de Ag. de 2024
0 votos
I would experiment with the settings controllable using the Import Tool to try to import the data. You could try running Import Tool with your smaller data set, import the data, and check that it imported as you expected. Then generate a script and run it on the larger file.
The conditions you identified that could make it difficult to read are:
- All columns are not always there
- Sometimes there is no space between the columns
- It uses -998 or -998.0 for NaN;
- There are blank lines.
If you specify that the file is fixed-width I think the second condition is okay. Unimportable cell handling may take care of the first condition. I think the blank lines will be imported as a row of unimportable cells, which you could repair after the fact with the missing-handling functions like rmmissing. Once you've imported the data, you could use standardizeMissing to replace -998 with NaN.
1 comentario
dormant
el 29 de Ag. de 2024
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!