Parsing a complex text file
Show older comments
I want to parse a complex text file (attached). The file has repetitive batches of:
pline3D = new TPolyLine3D(8,"");
Int_t ci; // for color index setting
TColor *color; // for color definition with alpha
ci = TColor::GetColor("#336699");
pline3D->SetLineColor(ci);
pline3D->SetLineWidth(2);
pline3D->SetPoint(0,101,101,-0.499);
pline3D->SetPoint(1,100.9983,101.0039,-0.4983234);
pline3D->SetPoint(2,101.0006,100.9976,-0.4986736);
pline3D->SetPoint(3,100.9977,101.0087,-0.4988685);
pline3D->SetPoint(4,100.9977,101.0087,-0.4988685);
pline3D->SetPoint(5,100.9937,101.0018,-0.4992562);
pline3D->SetPoint(6,100.9937,101.0018,-0.4992562);
pline3D->SetPoint(7,101.0014,101.0041,-0.4994762);
pline3D->Draw();
Here in first line 8 is the size of the track. for every batch I want to this along with the coordinates provided inside SetPoint(#,#,#,#).
For example the first track is of size 8 and is consist of these coordinates. The file is pretty big and has different track sizes. I would be grateful of any help I can get here.
Thanks very much for your time!
Accepted Answer
More Answers (1)
Walter Roberson
on 11 Sep 2020
You can use fileread() to read the entire file in as a character vector. Then use regexp() searching for
'new TPolyLine3D.*?Draw\(\);'
That will give you back a cell array in which each entry is a chunk of the file.
From there you can regexp() the cell array matching on
(?<=SetPoint\(\d+,)(?<x>[^,]+),(?<y>[^,]+),(?<z>[^\)])
and giving the option 'names' . The output for each cell entry will be a struct array with fields x, y, and z, which will be the text representations of the numbers. You can structfun(@str2double) or something similar to get the numeric values.
1 Comment
Sanchit Sharma
on 12 Sep 2020
Categories
Find more on String Parsing 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!