Extracting coordinates from a text file
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Pelajar UM
el 25 de Ag. de 2022
Comentada: Walter Roberson
el 25 de Ag. de 2022
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 comentarios
Respuesta aceptada
Walter Roberson
el 25 de Ag. de 2022
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 comentarios
Walter Roberson
el 25 de Ag. de 2022
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Other Formats en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!