필터 지우기
필터 지우기

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