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!