.csv file read and computation of content of file

2 views (last 30 days)
I have two .csv file which contains latitude and longitude in both the files. I need to read those values and calculate Haversine formula and plot.
  1 Comment
Manoj Pai
Manoj Pai on 9 May 2017
Edited: Walter Roberson on 9 May 2017
lat1=15.850388;
lon1=74.498652;
lat2=15.850254;
lon2=74.498539;
lat3=15.850404;
lon3=74.498500;
R = 6371; % Earth's radius in km
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
%fprintf(' %d \n',delta_lon);
a = sin(delta_lat/2)* sin(delta_lat/2) + cos(lat1) * cos(lat2) * sin(delta_lon/2)* sin(delta_lon/2);
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
fprintf(' %d \n',km);
delta_lat1 = lat3 lat2;
delta_lon1 = lon3 lon2;
a1 = sin(delta_lat1/2)* sin(delta_lat1/2) + cos(lat2) * cos(lat3) * sin(delta_lon1/2)* sin(delta_lon1/2);
c1 = 2 * atan2(sqrt(a1), sqrt(1-a1));
km1 = R * c1;
fprintf(' %d \n',km1);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2017
latlong1 = csvread('FirstFile.csv');
latlong2 = csvread('SecondFile.csv');
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
R = 6371; % Earth's radius in km
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
%fprintf(' %d \n',delta_lon);
a = sin(delta_lat/2) .* sin(delta_lat/2) + cos(lat1) .* cos(lat2) .* sin(delta_lon/2) .* sin(delta_lon/2); %notice each * was replaced by .*
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
fprintf(' %d \n',km);
  30 Comments
Walter Roberson
Walter Roberson on 9 Sep 2017
You had / in two places where you needed ./
You had * in some places where you needed .*
However: you are treating m (the vector of distances) as if it were a scalar, but it is a vector. When you go to print out the distances, you are printing out 599 distances at the same time, which is pretty much useless. When you test m<=30 you are testing whether all m<=30 because of the way that vectorized "if" works; you can change that to any(m<=30) to at least be more correct.
Manoj Pai
Manoj Pai on 9 Sep 2017
In the above code I am predicting the future points from previous points(which is in csv file) and if future points distance is less than 30m then printing alert and msgbox. Vx = sind(heading1).*speed1; Vy = cosd(heading1).*speed1 ; Lat2_HV = (lat1 + (T .* Vy/111));% T is in hours lat1=Lat2_HV;
Lon2_HV = lon1 +( T .* (Vx ./ cosd(Lat2_HV))./111);% 1 degree of lattitude/longitude corresponds to 111km
lon1=Lon2_HV;
%Calculation of fiture points for RV
Vx1 = sind(heading2).* speed2;
Vy1 = cosd(heading2).*speed2;
Lat2_RV = (lat2 + (T .* Vy1./111)); % T is in hours
lat2=Lat2_RV;
Lon2_RV = lon2 +( T * (Vx1 ./ cosd(Lat2_RV))./111); % 1 degree of lattitude/longitude corresponds to 111km
lon2=Lon2_RV;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!