Borrar filtros
Borrar filtros

What is the best way to read in a txt file that is organized with different sections from top to bottom?

1 visualización (últimos 30 días)
I currently have been using the textscan function to read in a set of data. The problem is that will result in lots of hard coding and delimiters for sorting through the data and assigning variables. I am interested in using the readtable function but that reads text from left to right.
Note- Each section name on a new line starts with '#'. To give a preview of the data:
# number of x points
2
#coordinates of points
# x y z
0.0 0.1 0.2
0.4 0.3 0.6
  1 comentario
José-Luis
José-Luis el 11 de Sept. de 2017
Editada: José-Luis el 11 de Sept. de 2017
Do you generate the text file yourself? If yes, then you could put it in whatever structured format you want and avoid this problem. There are very many possibilities and the better approach depends on the nature of your problem: xml, json, databases. There's even an API to create .mat files from other languages.
In this case, you might need a custom parser.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 11 de Sept. de 2017
readtable is not designed for this kind of files and will not work.
With these kind of file, unfortunately, you do have to write your own parser, using lower level functions such as textscan, sscanf, and co.
It does not mean that you can't avoid most hard coding if you design your code efficiently. If you show us your current code, we may be able to suggest improvements. Note that the most important thing is defining correctly the grammar of the text file.

Categorías

Más información sobre Data Import and Export 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!

Translated by