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?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Fifteen12
on 23 Feb 2023
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 Comments
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!