Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comment
José-Luis
José-Luis on 11 Sep 2017
Edited: José-Luis on 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.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 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.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!