Read a .txt or .dat file with multiple headers and commas?

6 views (last 30 days)
Hi everyone,
So I'm dealing with several .dat files (which I can change to .txt if that'd be easier). I need to read all of the numbers and assign them to separate arrays based on the headers.
Here's a sample:
Fit Curve Aui
Xbit, Hz, Alg_lin
2, 5, 102
1, 1
0.110010, -0.002008, -0.044201, -0.002580, -0.003818, -0.000605, -0.000053, -0.000200,
0.000545, 0.000721,...
1, 2
0.241561, -0.000270, -0.070538, -0.002198, -0.004259, -0.000619, -0.000900, -0.000267,
-0.000049, 0.000532,...
1,3 ...
1,4 ...
1,5 ...
2,1 ... etc.
This goes on 5 times, twice. I need to ignore the initial header (first three lines) and pass the other sub headers (1,1), (1,2), and so on. I then need to save the data to a structure/large array. There are 204 points per sub header that need to be saved.
struct(1,204,1,1) = 0.110010, -0.002008, -0.044201, -0.002580, -0.003818,...
struct(1,204,1,2) = 0.241561, -0.000270, -0.070538, -0.002198, -0.004259,...
I've been trying to use fscanf but I'm not very familiar with reading files, but need to extract this specific data. Can anyone lend me a hand?
Thanks in advance!
  1 Comment
per isakson
per isakson on 20 Aug 2017
Edited: per isakson on 20 Aug 2017
"[...] .dat files, which I can change to .txt if that'd be easier" No need, .dat is fine.
"There are 204 points per sub header" I assume that these 204 data points take up several lines. You show a line with 8 columns. However, 204/8 is not a whole number.
  • How many lines?
  • Do all the lines have the same number of data points?
  • How many data points per line?

Sign in to comment.

Answers (1)

Krishna Bindumadhavan
Krishna Bindumadhavan on 21 Aug 2017
I have attached a MATLAB script file along with the corresponding data file (.txt) which extracts the data and stores it in an array which is indexed by the various sub-headers.
You may have to make some minor changes to the script (ex. specify the correct path of the data file , changing the number of data points to be read, etc.) to suit your particular application. The changes that have to be made are mentioned in the comments in the script.
Hope this helps!


Find more on Entering Commands 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!