Extracting coordinates from a text file
1 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Other Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!