How to set a loop for calculate the distance between a certain location and a lot of data in csv.file using latitude and longtitude?
2 次查看(过去 30 天)
显示 更早的评论
I need help for calculate the distance between two location using the function ''arclen''.(like image.)
The problem is that I have a csv. file it have 600 data of locations, and the second row of the file is latitude, the third row is longtitude. How can I set a loop to calculate the distance between each data and this certain location (latitude:23.33°N, longtitude:122.22°E).
Thank you!
0 个评论
回答(1 个)
Fifteen12
2023-2-23
You'll need to import the CSV file. You can see this answer for more information: https://www.mathworks.com/matlabcentral/answers/72545-how-to-import-csv-file-in-matlab#answer_238222
Once you have the CSV file as a table you can use a for loop (simple solution) likeso:
%% Make example table
Location = {'New York'; 'Norfolk'; 'Cape St. Vincent'};
Latitude = [43.00; 37; 37];
Longitude = [-75; -76; -9];
example_table = table(Location, Latitude, Longitude);
%% Iterate through using arclength
for i = 1:height(example_table)
city_loc = [example_table.Latitude(i), example_table.Longitude(i)];
arclen(i) = distance('gc', [23.33, 122.2], city_loc);
end
arclen
Note that arclen is not a function, it's just an arbitrary name for a variable. Your teacher (or whoever gave you this problem) could have named it anything. The function is the distance function with the greatcircle parameter called. You can see more about the function here: https://www.mathworks.com/help/comm/ref/txsite.distance.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Other Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!