Finding the distance traveled using GPS Coordinates

11 vues (au cours des 30 derniers jours)
Mary Jean Savitsky
Mary Jean Savitsky le 10 Oct 2020
I am trying to calculate the distance traveled using GPS coordinates from a run. In my first attempt, I am using this code I found. I am extracting data from a google sheet containing the Times, Acceleration X, Y, & Z, latitude and longitude values. I am now getting the error:
Undefined function 'cos' for input arguments of type 'char'.
Error in project (line 66)
a=sin((deltaLat)/2)^2 + cos(lat1)*cos(lat2) * sin(deltaLon/2)^2;
Please let me know how to fix this error OR if you have any other suggestions on how to calculate distance using the 4650x60 sheet I am pulling the data from
% DOCID='1dqtn5aTdOIuhcLbz1coBazoInQ9a2XNJcb4rUf1BdqM';
% Outdoor_Data= GetGoogleSpreadsheet(DOCID);
% x1=Outdoor_Data(:,2); %the first runs latitude values
% y1=Outdoor_Data(:,3); %the first runs longitude values
% x2=Outdoor_Data(:,8); %the second runs latitude values
% y2=Outdoor_Data(:,9); %the second runs longitude values
% x3=Outdoor_Data(:,14); %the third runs latitude values
% y3=Outdoor_Data(:,15); %the third runs longitude values
% x4=Outdoor_Data(:,20); %the fourth runs latitude values
% y4=Outdoor_Data(:,21); %the fourth runs longitude values
% x5=Outdoor_Data(:,26); %the fifth runs latitude values
% y5=Outdoor_Data(:,27); %the fifth runs longitude values
function [d1km d2km]=lldistkm(latlon1,latlon2)
% format: [d1km d2km]=lldistkm(latlon1,latlon2)
% Distance:
% d1km: distance in km based on Haversine formula
% (Haversine: http://en.wikipedia.org/wiki/Haversine_formula)
% d2km: distance in km based on Pythagoras’ theorem
% (see: http://en.wikipedia.org/wiki/Pythagorean_theorem)
% After:
% http://www.movable-type.co.uk/scripts/latlong.html
%
% --Inputs:
% latlon1: latlon of origin point [lat lon]
% latlon2: latlon of destination point [lat lon]
%
% --Outputs:
% d1km: distance calculated by Haversine formula
% d2km: distance calculated based on Pythagoran theorem
%
% --Example 1, short distance:
% latlon1=[-43 172];
% latlon2=[-44 171];
% [d1km d2km]=distance(latlon1,latlon2)
% d1km =
% 137.365669065197 (km)
% d2km =
% 137.368179013869 (km)
% %d1km approximately equal to d2km
%
% --Example 2, longer distance:
% latlon1=[-43 172];
% latlon2=[20 -108];
% [d1km d2km]=distance(latlon1,latlon2)
% d1km =
% 10734.8931427602 (km)
% d2km =
% 31303.4535270825 (km)
% d1km is significantly different from d2km (d2km is not able to work
% for longer distances).
%
% First version: 15 Jan 2012
% Updated: 17 June 2012
%--------------------------------------------------------------------------
DOCID='1dqtn5aTdOIuhcLbz1coBazoInQ9a2XNJcb4rUf1BdqM';
Outdoor_Data= GetGoogleSpreadsheet(DOCID);
x1=Outdoor_Data{:,2}; %the first runs latitude values
y1=Outdoor_Data{:,3}; %the first runs longitude values
radius=6371;
lat1=x1(2);
lat2=x1(3);
lon1=y1(2);
lon2=y1(3);
deltaLat=lat2-lat1;
deltaLon=lon2-lon1;
a=sin((deltaLat)/2)^2 + cos(lat1)*cos(lat2) * sin(deltaLon/2)^2;
c=2*atan2(sqrt(a),sqrt(1-a));
d1km=radius*c; %Haversine distance
x=deltaLon*cos((lat1+lat2)/2);
y=deltaLat;
d2km=radius*sqrt(x*x + y*y); %Pythagoran distance
end

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Oct 2020
GetGoogleSpreadsheet() from File Exchange returns cell arrays of character vectors. It makes no attempt at all to convert to numeric.
x1 = str2double(Outdoor_Data(:,2)); %the first runs latitude values
y1 = str2double(Outdoor_Data(:,3)); %the first runs longitude values

Plus de réponses (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by