In matlab
Write a function gpxmeanheartrate that takes the name of a GPX file and returns the average heart rate of the tracking samples stored in that file. GPX (GPS Exchange Format) is an XML format used for sharing GPS tracking data. The heart rate information is enclosed by “<gpxtpx:hr>” and “</gpxtpx:hr>”, e.g., one of the time points where the heart rate is 147 will contain the text: “<gpxtpx:hr>147</gpxtpx:hr>”. Sample GPX files used in this problem are available at @/ftp/jogging.gpx and @/ftp/jogging_small.gpx.
Example:
>> gpxmeanheartrate('jogging_small.gpx') ans = 149 >> gpxmeanheartrate('jogging.gpx') ans = 156.0452
Expert Answer
fileid = fopen(filename)
sum = 0;
count = 0;
tline = fgetl(fileid);
while ischar(tline) %read file line by line
start = findstr(‘<gpxtpx:hr>’, tline); %find in the tag is found
if size(start) > 0 %if found
stop = findstr(‘</gpxtpx:hr>’, tline); %find the endex of terminator
for i = 1:numel(start) %this takes care if single line contains multiple tags
sum = sum + str2num(tline(start(i)+11:stop(i)-1)); %simple operation of extracting substring and add it to sum
count ++; %increase count
end
end
tline = fgetl(fileid);
end
fclose(fileid); %close file to prevent data loss
avg = sum/count; %find avg
end