Do I need nested loops?

1 Ansicht (letzte 30 Tage)
Bob Murphy
Bob Murphy am 17 Mär. 2016
Kommentiert: Bob Murphy am 22 Mär. 2016
I am trying to create a small script that will allow me to expand the perimeter based off coordinates. The program is running through the variables correctly when I look at the workspace, but I can't get it to write the results to the structure multiple times. It will only write to the structure once. Am I missing a nested loop?
load 'lat_lon.mat'
new_coord = [];
for i = 1:length(lat_lon)
1
new_coord.Lat = [lat_lon(:,[1])];
new_coord.Lon = [lat_lon(i:,[2])];
2
%Finds the radius of the Earth at given latitude
radius = 6372 * cos(lat_lon(i,[1])/180 * pi)
3
%Converts given perimeter expansion to degrees
lat_nat_deg = nm2deg(3.5, radius);
new_coord.lat_exp = [lat_nat_deg];
nat_deg = nm2deg(3.5);
new_coord.lon_exp = [nat_deg];
4
new_coord.latitude = [lat_lon(i,[1]) + lat_nat_deg];
5
new_coord.longitude = [lat_lon(i,[2]) + nat_deg]
end
The lat_lon.mat is :
Lat_Long = [60.8427194, -042.7969500; ...
60.8646972, -042.5323889; ...
60.7640417, -042.5178639; ...
60.5882139, -042.8185056; ...
60.8080111, -042.8507500; ...
60.8069889, -042.8374167; ...
60.8083028, -042.8278361; ...
60.8114056, -042.8186167; ...
60.8152306, -042.8117389; ...
60.8203694, -042.8055667; ...
60.8256528, -042.8013611; ...
60.8315167, -042.7985083; ...
60.8374056, -042.797078; ...
60.8427194, -042.7969500];

Akzeptierte Antwort

Chad Greene
Chad Greene am 17 Mär. 2016
The loop you've written overwrites the results each time through the loop. Any information you want to log such as newcoord.latitude must be indexed to i. (Better yet, change i to k because i is the imaginary unit in Matlab and defining i as a variable can occasionally cause errors.)
Here are two ways you can organize your structure:
for k = 1:10
newcoord.lat{k} = randi(90,1,2);
anotherway(k).lat = randi(90,1,2);
end
  1 Kommentar
Bob Murphy
Bob Murphy am 22 Mär. 2016
That's exactly what I was trying to do! If I wanted to save the results as a double like the provided coordinates were, is there a simple edit to do that?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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!

Translated by