.csv file read and computation of content of file
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
Manoj Pai
2017 年 5 月 9 日
編集済み: Walter Roberson
2017 年 5 月 9 日
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);
採用された回答
Walter Roberson
2017 年 5 月 9 日
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 件のコメント
Manoj Pai
2017 年 5 月 9 日
Thank you Walter
Manoj Pai
2017 年 5 月 9 日
I need to plot these values. Please guide me..
Manoj Pai
2017 年 5 月 9 日
編集済み: Walter Roberson
2017 年 5 月 9 日
f1=figure(1);
cla;
hold on;
plot(lon1,lat1,lon2,lat2,'k-','marker','+')
%plot(lon2,lat2,'.g','MarkerSize',10)
plot_google_map
%is this way we can do?
Walter Roberson
2017 年 5 月 9 日
That looks possible. I do not know plot_google_map though.
Manoj Pai
2017 年 5 月 9 日
ok. It works plot_google_map. If we run the plot_google_map function. The .csv file has (ie FirstFile.csv and SecondFile.csv) contains multiple data(it contains of about 500 values of lat and long). I need to calculate Haversine formula for each data(lat long) of the .csv file. Does the solution provided above by you works?
Walter Roberson
2017 年 5 月 9 日
Yes. With the change from * to .* that I pointed out, all of the values will be computed at the same time, provided that you are comparing the corresponding entries in each set. (If you want to take the distance from each member of the first file to every member of the second file, and so on, then the code would have to change a bit.)
Manoj Pai
2017 年 5 月 9 日
Samples of csv file data First file lat lon 15 74 16 75 .. ..
Second file lat lon 17 76 18 77 .. .. I need to calculate first entry of lat lon of firstfile.csv (15 74) with first entry of secondfile.csv(17,76) and second entry of firstfle.csv (16 75) with second entry of secondfile.csv(18 77) and so on... Could you please tell me what changes needs to be done?
These are the two .csv files
Walter Roberson
2017 年 5 月 9 日
"and second entry of firstfle.csv (16 75) with second entry of secondfile.csv(18 77)"
That is already what the code does.
Manoj Pai
2017 年 5 月 9 日
Thank you...
Manoj Pai
2017 年 5 月 11 日
beep; msgbox('Danger, Will Robinson!') I need this message when there is the intersection of lat, long of both the files. I have shared the output of the code. The point at which there is intersection an alert message should be popped up.
if any(km == 0); beep; msgbox('Danger, Will Robinson!'); end
Manoj Pai
2017 年 5 月 12 日
編集済み: Walter Roberson
2017 年 5 月 12 日
I have those lat, long values in radians.
How to calculate the trigonometric functions for radians values.
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
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));
Walter Roberson
2017 年 5 月 12 日
sin() and cos() and atan2() automatically work with radians.
You would have needed to do something special to work with degrees -- you would have had to use sind() and cosd() and atan2d()
Manoj Pai
2017 年 5 月 12 日
ok Thank You
Manoj Pai
2017 年 9 月 6 日
How to plot lat1 lon1 and lat2 lon2 on a single graph
plot(lon1,lat1,lon2,lat2,'k-','marker','+')
hold on
plot(lon2,lat2,'.g','MarkerSize',10)
Manoj Pai
2017 年 9 月 7 日
Thanks a lot
In this file first row consists of title as lat1, lon1...... Should I need to do some changes in this part of the code?
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
latlong1 = csvread('FirstFile.csv', 1, 0);
Note that this requires R2014a or so (not sure of exact first release)
latlong1= csvread('C:\Users\Desktop\FirstFile .csv',1,0);
Manoj Pai
2017 年 9 月 8 日
Can we use this for file in different directory
Walter Roberson
2017 年 9 月 8 日
Sure, reading from a different directory is no problem, in exactly the way you posted. Just watch out: I see a space between 'FirstFile' and '.csv' that should probably not be there.
Manoj Pai
2017 年 9 月 9 日
I am new to Matlab.Thank you I learnt a lot from you.
Manoj Pai
2017 年 9 月 9 日
編集済み: Walter Roberson
2017 年 9 月 9 日
clear
clc
HV = csvread('FirstFile.csv',1,0);
RV = csvread('SecondFile.csv',1,0);
lat1 = HV(:,1);
lon1 = HV(:,2);
speed1 = HV(:,3);
heading1 = HV(:,4);
lat2 = RV(:,1);
lon2 = RV(:,2);
speed2 = RV(:,3);
heading2 = RV(:,4);
R = 6378.137; %Earth's radius in km
KMPH_TO_MPS = 0.277778; %conversion from kmph to mps
iteration = 200; %iteration for 200 values
T = 1;
for i=0:iteration
% %Calculation of future points for HV
Vx = sind(heading1).*speed1;
Vy = cosd(heading1).*speed1 ;
Lat2_HV = (lat1 + (T * Vy/111));% T is in hours
lat1=Lat2_HV;
Long2_HV = lon1 +( T * (Vx / cosd(Lat2_HV))/111);% 1 degree of lattitude/longitude corresponds to 111km
lon1=Long2_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;
Long2_RV = lon2 +( T * (Vx1 / cosd(Lat2_RV))/111); % 1 degree of lattitude/longitude corresponds to 111km
lon2=Long2_RV;
delta_lat = (Lat2_HV - Lat2_RV ) ; % Difference between latitudes of HV and RV
delta_lon = (Lon2_HV - lon2_RV) ; % Difference between longitudes of HV and RV
a = sind(delta_lat/2) .* sind(delta_lat/2) + cosd(Lat2_RV) .* cosd(Lat2_HV) .* sind(delta_lon/2) .* sind(delta_lon/2); %notice each * was replaced by .*
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
m = km * 1000;
plot(lat1,lon1,'*',lat2,lon2,'+',Lat2_HV,Lat2_RV,'b--',Lon2_HV,Lon2_RV,'r--'); hold on
drawnow
fprintf('Distance between vehicles= %f \n',m);
if(m<=30) % Checking safety distance for alert condition
% Calculation for relative velocity
HV1=((speed1)*(cosd(heading1)));
HV2=((speed1)*(sind(heading1 )));
RV1=((speed2)*(cosd(heading2)));
RV2=((speed2)*(sind(heading2)));
s1=(HV1)+(RV1);
s2=(HV2)+(RV2);
R_speed=(s1.^2)+(s2.^2);
R_speed1=(R_speed.^(1/2));
fprintf('RELATIVE_SPEED= %f \n',R_speed1); %Display relative velocity of HV and RV
% calculation of TTC with predicted trajectory of HV and RV with relative speed of HV and RV
TTC=dm2/(R_speed1* KMPH_TO_MPS);
fprintf('TTC= %f \n',TTC);
msgbox('IMA ALERT');
fprintf('ALERT');
else
fprintf('safe');
end
end
%plot(lon1,lat1,lon2,lat2,'k-','marker','+')
%hold on
%plot(lon2,lat2,'.g','MarkerSize',10)
Manoj Pai
2017 年 9 月 9 日
編集済み: Walter Roberson
2017 年 9 月 9 日
Error using +
Matrix dimensions must agree.
Error in calculation (line 29)
Long2_HV = lon1 +( T * (Vx / cosd(Lat2_HV))/111);% 1 degree of lattitude/longitude corresponds to 111km
I am getting this error
Walter Roberson
2017 年 9 月 9 日
The files that you posted first time around only have two columns. You posted a new version of FirstFile.csv with more columns, but you did not post a new version of SecondFile.csv
Manoj Pai
2017 年 9 月 9 日
Yes it contains 4 columns in both the files.. lat1, lon1, speed1, heading1
Walter Roberson
2017 年 9 月 9 日
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.
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;
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Coordinate Reference Systems についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
