gpx2mat.m 1.48 KB
function gpx2mat(gpxfile)
% GPX2MAT - Convert gpx files to mat files
%
% Syntax:  gpx2mat(gpxfile)
% 
% Inputs:
%    gpxfile - The name of the gpx file to convert, excluding the .gpx
%              extension
% 
% Example:
%    g_interp('spot_device')
% 
% Other m-files required: gpxread
% Subfunctions: none
% 
% See also: GPXREAD, TR_MAP, TR_MERGE

% Author: Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% September 2014
% ______________________________________________________________________
% 

curdir = pwd;
matdir = [curdir,'/mat'];

file = dir(['./',gpxfile,'.gpx']);

wpt = gpxread(file.name);

lat   = wpt.Latitude;
lon   = wpt.Longitude;
clock = wpt.Time;

trk = gpxread(file.name,'FeatureType','track','Index',1);

ind = 0;
while ~isempty(trk)
    ind = ind + 1;
    trk = gpxread(file.name,'FeatureType','track','Index',ind);
    if ~isempty(trk)
        lat   = [lat   trk.Latitude];
        lon   = [lon   trk.Longitude];
        clock = [clock trk.Time];
    end
end

N = length(lat);

for n = 1:N

    yr(n) = str2double(clock{n}(1:4));
    mo(n) = str2double(clock{n}(6:7));
    da(n) = str2double(clock{n}(9:10));
    hh(n) = str2double(clock{n}(12:13));
    mm(n) = str2double(clock{n}(15:16));
    ss(n) = str2double(clock{n}(18:19));

    time(n) = datenum([yr(n) mo(n) da(n) hh(n) mm(n) ss(n)]);
end

data = [time' lon' lat'];

data = sortrows(data,1);

if ~exist(matdir,'dir')
    mkdir(matdir)
end

save([matdir,'/',gpxfile],'data');