Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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 commentaire
José-Luis
José-Luis le 11 Sep 2017
Modifié(e) : José-Luis le 11 Sep 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.

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 11 Sep 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.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by