Clear Filters
Clear Filters

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 views (last 30 days)
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!

Answers (1)

Fifteen12 on 23 Feb 2023
You'll need to import the CSV file. You can see this answer for more information:
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);
arclen = 1×3
111.8042 117.2785 104.1646
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:

Community Treasure Hunt

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

Start Hunting!