MATLAB: .csv file read and computation of content of file

csvhaversineplot

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.

Best Answer

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);