MATLAB Answers

Saving and appending for loop results from every run

5 views (last 30 days)
Aladdin
Aladdin on 30 Aug 2021
Commented: Aladdin on 31 Aug 2021
Hi everyone,
I have a dataset with countries and lat/lon columns. I want to measure the distance between the countries. I used the distance function from the mapping toolbox and a for loop to get every single country.
I’m struggling with saving the results from the for loop. I’m overwriting my results. I want to save every run from the for loop as a single column, basically append the results from every run. Does anyone have an efficient/better way to save the results? I would really appreciate your help in advance. I attached the data example and my code
format long g
Data = readtable('Test.xlsx');
for i=1:Data.lat
Distance = deg2sm(distance([Data.lat(i) Data.lon(i)], [Data.lat(1:end) Data.lon(1:end)]));
Data.Distance = Distance
end
  2 Comments
Aladdin
Aladdin on 30 Aug 2021
@DGM I agree with you it won't fit in one column. I was trying to save each run as a new column and I will deal witrh the rest later like puting it in a matrix where the column header and the row name is the country if that make sense. for now, any ideas how to save each run as a new column?

Sign in to comment.

Accepted Answer

Chunru
Chunru on 31 Aug 2021
If you want to attach the distance to the Data table you have read, then you can attach the distance vector for each country as follows.
format long g
Data = readtable('Test.xlsx');
for i=1:length(Data.lat)
Distance = deg2sm(distance([Data.lat(i) Data.lon(i)], [Data.lat(1:end) Data.lon(1:end)]));
Data.Distance{i} = Distance;
end
Data
Data = 19×4 table
lat lon country Distance __________ ___________ ________________________ _____________ 42.546245 1.601554 {'Andorra' } {19×1 double} 23.424076 53.847818 {'United Arab Emirates'} {19×1 double} 33.93911 67.709953 {'Afghanistan' } {19×1 double} 17.060816 -61.796428 {'Antigua and Barbuda' } {19×1 double} 18.220554 -63.068615 {'Anguilla' } {19×1 double} 41.153332 20.168331 {'Albania' } {19×1 double} 40.069099 45.038189 {'Armenia' } {19×1 double} 12.226079 -69.060087 {'Netherlands Antilles'} {19×1 double} -11.202692 17.873887 {'Angola' } {19×1 double} -75.250973 -0.071389 {'Antarctica' } {19×1 double} -38.416097 -63.616672 {'Argentina' } {19×1 double} -14.270972 -170.132217 {'American Samoa' } {19×1 double} 47.516231 14.550072 {'Austria' } {19×1 double} -25.274398 133.775136 {'Australia' } {19×1 double} 12.52111 -69.968338 {'Aruba' } {19×1 double} 40.143105 47.576927 {'Azerbaijan' } {19×1 double}
If you want to form a tall vector of distances, or a paired distance matrix you can do the following:
d = [];
for i=1:length(Data.lat)
Distance = deg2sm(distance([Data.lat(i) Data.lon(i)], [Data.lat(1:end) Data.lon(1:end)]));
d = [d; Distance]; % tall vector
%d = [d Distance]; % matrix
end
d
d = 361×1
0 3243.52727137724 3545.36144853792 4082.36333070285 4095.96109655197 958.474221225425 2236.15594470842 4668.31071718444 3854.00859782759 8139.3428702947
  1 Comment
Aladdin
Aladdin on 31 Aug 2021
@Chunru Thank you so much for your time and help! I really appreciate it. Neat and easy to follow.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by