- How many lines?
- Do all the lines have the same number of data points?
- How many data points per line?
Read a .txt or .dat file with multiple headers and commas?
4 次查看(过去 30 天)
显示 更早的评论
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.
E.g.
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 个评论
per isakson
2017-8-20
编辑:per isakson
2017-8-20
"[...] .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.
回答(1 个)
Krishna Bindumadhavan
2017-8-21
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!