How to get details from header

11 次查看(过去 30 天)
kubyk
kubyk 2013-11-5
编辑: dpb 2013-11-7
I have document which contains header and data. One part of header is useless so I skipped this but in the second part are some details which I would like to save as variables.
the first part of header
ABR Group Header ==================================
Group Number: 2
Records: 16
SigGen File #1: D:\data\BioSigData\signaly\Stdpipji33.sig
SigGen File #2:
Subject ID: LEcisty
Reference #1: usporadani1
Reference #2: ABR4ch
Memo:
Start Time: Mon Apr 08 11:14
End Time: Mon Apr 08 11:16
----------------------------------------------------
the second part of header
Record Number: 24
Aqu. Duration: 19.9885 ms
Onset Delay: 0 ms
No. Points: 488
No. Averages: 300
No. Artifacts: 0
Start Time: Mon Apr 08 11:15
SigGen Index: 253
Variables:
Frequency = 7999.98 Hz
Attenuation = 0 dB
Phase = 180 DegredB
Calibration = 0 dB
data
1.18025e-005
1.04013e-005
9.32095e-006
9.15802e-006
9.79093e-006
1.04253e-005
1.02769e-005
9.14981e-006
7.38618e-006
5.5122e-006
3.99292e-006
3.07316e-006
2.70889e-006
.
.
.
I would like save inormation about duration, frequency and number of points. I don't know, how to get this details from header. Regards.

采纳的回答

dpb
dpb 2013-11-6
Looks like a fixed format so just count lines and use the textscan ability to be called multiple times on same file...
fid=fopen(filename,'r');
dur=textscan(fid,'Aqu. Duration: %f ms','headerlines',15);
pts=textscan(fid,'No. Points: %f','headerlines',1);
frq=textscan(fid,'Frequency = %f Hz','headerlines',1);
fid=fclose(fid);
May want to cast to variable from cell.

更多回答(1 个)

kubyk
kubyk 2013-11-6
So I get details from head and the data with this code:
filename = 'usporadani11.txt';
fid = fopen (filename, 'r');
dur=textscan(fid,'Aqu. Duration: %f ms','headerlines',14);
dur=dur{1};
pts=textscan(fid,'No. Points: %f','headerlines',1);
pts=pts{1};
frq=textscan(fid,'Frequency = %f Hz','headerlines',6);
frq=frq{1};
att=textscan(fid,'Attenuation = %f dB');
att=att{1};
fs=pts/(dur/1000);
data=textscan(fid, '%f', 'headerlines', 2);
data=data{1};
fclose(fid);
But I can have several this sets in one file. It looks: header1, data1, header2, data2...header16, data16. Still the same format. Is it possible to get this ? Regards
  1 个评论
dpb
dpb 2013-11-6
编辑:dpb 2013-11-7
If it's fixed length or you can count the lines based on input in the headers, just keep on doing the same thing in a loop.
You can allocate additional cell arrays and concatenate the results or use named dynamic fields in a structure or convert the cell contents of the textscan call to an array and concatenate as storage mechanism depending on needs.
OBTW, you can save a step on the conversion--instead of
dur=textscan(fid,'Aqu. Duration: %f ms','headerlines',14);
dur=dur{1};
can write
dur=cell2mat(textscan(fid,'Aqu. Duration: %f ms','headerlines',14));
Would be nice to have the facility to return variables instead of only cells from textscan, but not to be... :(

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by