How do I specify the number of header lines using TEXTSCAN to read a file that has multiple blocks of data, each with its own header, in MATLAB 7.3 (R2006b)?
57 次查看(过去 30 天)
显示 更早的评论
I have a text file that consists of:
Dataset 1:
5 2 7
2 2 1
Dataset 2:
8 7 8
0 1 3
I would like to read the numeric information, but skip the header line at the beginning of each block of data.
采纳的回答
MathWorks Support Team
2009-6-27
This bug has been fixed in Release 2007b (R2007b). For previous product releases, read below for any possible workarounds:
Execute the following commands:
fid = fopen('mydata.txt');
data1 = textscan(fid, '%d%d%d', 2, 'headerLines', 1);
data2 = textscan(fid, '%d%d%d', 2, 'headerLines', 2);
fclose(fid);
The TEXTSCAN function is called once for each set of data, using the 'headerLines' option to skip the header lines.
There is an error in the documentation for TEXTSCAN within the MATLAB Function Reference, concerning the 'headerLines' option. It should read that the value of 'headerLines' is the number of line breaks that are skipped before reading commences.
In the example above, 'headerLines' is set to 2 on the second call to TEXTSCAN because the first call to TEXTSCAN leaves you just before the line break on the third line of "mydata.txt". Moving to the beginning of the next line counts as a header line.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!