필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
rbme17
rbme17 2017년 8월 18일
답변: Krishna Bindumadhavan 2017년 8월 21일
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
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.
  • How many lines?
  • Do all the lines have the same number of data points?
  • How many data points per line?

댓글을 달려면 로그인하십시오.

답변 (1개)

Krishna Bindumadhavan
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!

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by