Parsing a complex text file

조회 수: 13 (최근 30일)
Sanchit Sharma
Sanchit Sharma 2020년 9월 11일
댓글: Sanchit Sharma 2020년 9월 12일
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!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 11일
Try this code
f = fopen('line_plot_1.txt');
data = {};
while ~feof(f)
data_ = textscan(f, 'pline3D->SetPoint(%f,%f,%f,%f);');
if ~isempty(data_{1})
data{end+1} = [data_{:}];
else
fgetl(f);
end
end
fclose(f);
It creates a cell array, and each cell contains four columns corresponding to the values in files.
Result:
>> data{1}
ans =
0 101.0000 101.0000 -0.4990
1.0000 100.9983 101.0039 -0.4983
2.0000 101.0006 100.9976 -0.4987
3.0000 100.9977 101.0087 -0.4989
4.0000 100.9977 101.0087 -0.4989
5.0000 100.9937 101.0018 -0.4993
6.0000 100.9937 101.0018 -0.4993
7.0000 101.0014 101.0041 -0.4995
>> data{2}
ans =
0 101.0000 101.0000 -0.4990
1.0000 100.9977 100.9945 -0.4992
2.0000 101.0122 100.9999 -0.4996
>> data{6}
ans =
0 101.0000 101.0000 -0.4990
1.0000 101.0004 101.0058 -0.4987
2.0000 101.0051 101.0103 -0.4984
3.0000 101.0037 101.0054 -0.4995
4.0000 101.0037 101.0054 -0.4995
5.0000 101.0034 100.9967 -0.4995
6.0000 101.0034 100.9967 -0.4995
7.0000 101.0034 100.9967 -0.4995
8.0000 101.0034 100.9967 -0.4995

추가 답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by