Parsing a complex text file
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
Walter Roberson
2020년 9월 11일
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.
카테고리
도움말 센터 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!