Extracting coordinates from a text file

11 次查看(过去 30 天)
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