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

1 次查看(过去 30 天)
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 个评论
José-Luis
José-Luis 2017-9-11
编辑:José-Luis 2017-9-11
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.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2017-9-11
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.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by