text file with multiple section of headers

3 views (last 30 days)
Ham Man
Ham Man on 4 Sep 2022
Commented: dpb on 4 Sep 2022
I have a txt file data like bellow:(multiple section and each section with different column of data)
u v w p q
1 2 3 4 5
.
.
.
1 1 1 1 1
uu uv vv
4 3 8
.
.
.
1 1 1
I want to load each section of these set of data for plotting using fopen,textscan. How can I each section without calling fopen multiple time.
  1 Comment
dpb
dpb on 4 Sep 2022
"... without calling fopen multiple time."
As documented, textscan leaves the filepointer at the location of the last point read when either the number of items requested is satisfied or the conversion fails or reaches end of file. There's no need to close/reopen the file for multiple calls/reads.
BUT, with an irregular file structure such as that with variable number of variables in some records and no repeating pattern, it'll be very difficult to find a sequential format expression to keep the file pointer in synch and pick up for the next dataset. I think this is one that will be more well suited to simply use fgetl to read a line at a time and parse each record in turn. Or, use readlines (R2020b needed) to read the entire file into memory and do the same parsing except in memory.
The pasted data looks to have a lot of extra white space; hard to know what the real file really looks like -- attach it directly (or a representative portion if it is large).

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!