Parsing Complicated text file header
2 views (last 30 days)
Show older comments
I have a complicated header to a .txt file that I'm having trouble reading and parsing. I want to pull out the Sample, the Size, and the data (roughly 13000 lines). I've included a simplified version below.
What is the best way to read this?
Thank you!
Sample Virgin 1050, Sec 6, Data Sample
Geometry Rectangular (Length, Width, Thickness)
GeomFactor 27.573229 1/mm 0.041252 1/mm 1.137453 1/mm²
Size 24.2412 3.17500 0.276900 mm
SampParams 0.4000
Method Creep - Sample Rate
Comment 37 Deg, 10 MPa
Xcomment Air Bearing Gas: Air
Text
Multiplex None
InstCalDate Electronics: 2011-11-14 10:45:15 0.00006
AutoAnalysis Off
MacroFile
OrgFile C:\Folder\File
StartOfData
5.027167 37.02016 0.0000000 0.0000000
5.078333 37.02214 1762.800 0.0000000
5.127500 37.02239 2218.972 0.0000000
0 Comments
Accepted Answer
Matt Tearle
on 7 Dec 2011
If the headers are uniformly formatted, you could simplify this a bit, but this should work:
fid = fopen('data.txt');
sample = fgetl(fid)
foo = textscan(fid,'Size %f%f%f%*s','headerlines',2,'CollectOutput',true);
sizeinfo = foo{1}
foo = '';
while ~strcmp(foo,'StartOfData')
foo = fgetl(fid);
end
foo = textscan(fid,'%f%f%f%f','delimiter',',','CollectOutput',true);
data = foo{1}
fclose(fid);
I'm mixing-n-matching approaches. I'm assuming the first line is the "sample", and the fourth line is the "size". But for generality, rather than counting lines, I'm assuming the data starts after the line "StartOfData". Note the use of the HeaderLines property in textscan to skip fixed numbers of lines, and the use of fgetl and strcmp to hunt for a line with specific text.
More Answers (0)
See Also
Categories
Find more on Text Files 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!