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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I need help for calculate the distance between two location using the function ''arclen''.(like image.)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1304700/image.png)
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 Kommentare
Antworten (1)
Fifteen12
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Hypothesis Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!