xml2struct is very slow...
이전 댓글 표시
Hello community,
I use xmlread() and xml2struct() to read latitude,longitude and time vector from an .gpx-file (which is formated as xml). This solution appears to be very, very slow. xml2struct >getNodeData takes over 90 % of the time of my whole script according to the profiler. Here is an example of the call in my function:
DOM=xmlread(file);
GPX=xml2struct(DOM);
time=strings(size(GPX.gpx.trk.trkseg.trkpt))'; % preallocation
lat=strings(size(time));
lon=strings(size(time));
for b=1:length(GPX.gpx.trk.trkseg.trkpt) % extract parameters
time(b)=GPX.gpx.trk.trkseg.trkpt{1,b}.time.Text;
lat(b)=GPX.gpx.trk.trkseg.trkpt{1,b}.Attributes.lat;
lon(b)=GPX.gpx.trk.trkseg.trkpt{1,b}.Attributes.lon;
end
lat=str2double(lat);
lon=str2double(lon);
time=extractAfter(time,'T');
time=extractBefore(time,'Z');
% the time will be formatted later on
The main problem on speed is not the for-loop but the xml2struct() command.
Here is an example of my data opened in texteditor:
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="Polar Pro">
<metadata><author><name>Polar</name></author><time>2020-01-29T16:16:08.000Z</time></metadata>
<trk><trkseg>
<trkpt lat="-33.94066317" lon="18.86733817"><ele>0.0</ele><time>2020-01-29T16:16:10.000Z</time></trkpt>
<trkpt lat="-33.940 ….
Any suggestions to improve the speed? It does really matter, the function is going to be called several times...
EDIT 17.03.2020: Attached file as an example
답변 (1개)
Pierre Harouimi
2021년 12월 1일
Long time after... But hope this will help
GPX = readstruct("Example.xml");
time = [GPX.trk.trkseg.trkpt.time]';
lat = [GPX.trk.trkseg.trkpt.latAttribute]';
lon = [GPX.trk.trkseg.trkpt.lonAttribute]';
time = datetime(time,'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSS''Z');
[h,m,s] = hms(time);
댓글 수: 1
Anthony
2023년 11월 20일
Thank you, I missed this function when it was introduced and I was still using xml2struct.
카테고리
도움말 센터 및 File Exchange에서 Historical Contests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!