Extracting coordinates from a text file

조회 수: 4 (최근 30일)
Pelajar UM
Pelajar UM 2022년 8월 25일
댓글: Walter Roberson 2022년 8월 25일
I have a text file that contains various info. I am interested in the coordinates that come in the following format:
<SERIES_2D UniqueID="0" Name="Band energy" NumPoints="1760">
<POINT_2D XY="0.1526777587356064,-18.83713650553515"/>
<POINT_2D XY="0.2259585473519713,-18.77591633509903"/>
<POINT_2D XY="0.2992393359683362,-18.68414023009788"/>
<POINT_2D XY="0.3629792736759856,-18.65734926254393"/>
<POINT_2D XY="0.4267192113836351,-18.60265900778901"/>
<POINT_2D XY="0.5,-18.62938793338875"/>
</SERIES_2D>
where the value of NumPoints (in this case 1760) can vary.
atextfile = fileread ('textfile.txt');
a=strfind (atextfile,'POINT_2D XY="');
b= atextfile(:,a);
and b returns PPPPPPP... So this is clearly not working as intended.
I want to take the XY coordinates between <SERIES_2D UniqueID="0" Name="Band energy" NumPoints="1760"> and </SERIES_2D> and put them in an array.

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 25일
parts = regexp(atextfile, 'XY="(?<X>[^,]+),(?<Y>[^"]+)', 'names');
X = str2double({parts.X});
Y = str2double({parts.Y});
Unless, that is, there are other XY= in the file that need to be excluded.
  댓글 수: 2
Pelajar UM
Pelajar UM 2022년 8월 25일
편집: Pelajar UM 2022년 8월 25일
Thanks a lot, but yes, there are other XY that have to be excluded. I specifically need the ones between <SERIES_2D UniqueID="0" Name="Band energy" NumPoints="1760"> and </SERIES_2D> where 1760 is just an example. It could be any number. But the rest of it is always like this.
I addressed this by adding and it works:
findend = strfind (atextfile, '</SERIES_2D>');
atextfile = atextfile(1:findend(1,1));
Walter Roberson
Walter Roberson 2022년 8월 25일
sections = regexp(atextfile, '^<SERIES_2D.*?SERIESL_2D>', 'match');
parts = regexp(sections, 'XY="(?<X>[^,]+),(?<Y>[^"]+)', 'names');
Xs = cellfun(@(C) str2double({C.X}), parts, 'uniform', 0);
Ys = cellfun(@(C) str2double({C.Y}), parts, 'uninform', 0);
This code will break up the input into series, and then extract the X and Y within each series. The output will be a cell array Xs and a cell array Ys, each with one entry per section, containing the coordinates for that section.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Analytics Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by