Problems with Conditionally execute statements

1 view (last 30 days)
Hi, I'm having problems using if, elseif, and else. I'm trying to execute the statements for a matrix of satelite wind data... I pass from cartesian to polar coordinates and then from radians (on angle theta, the direction of the wind vector) to degrees, and I realized that matlab points the zero towards east in the rose (compass function). I want to make a conversion so that a theta angle of zero (or 360) points north, and 90 ° points east. I wrote this lines of code to do that... the weird thing is that the conditionall statements work when I use single values of theta, but when I run it for the whole matrix, it doesn't work, and it uses THE LAST (ELSE) statement, instead of my ELSEIF.. Any help with that? or any other way to make the conversion that I'm trying to do? Any matlab function to do it? Thanks a lot!
if tm < 0
tm = abs(tm)+90;
elseif tm > 90
tm=360+90-(tm);
else
tm=abs(tm-90);
end
My data matrix (tm, already in degrees):
tm = [-159.3884 -173.0609 -174.9012 -178.0744 164.3687 -179.7163 NaN NaN NaN
-161.7762 -179.5870 173.2305 176.2090 158.6573 177.0568 NaN NaN NaN
-163.5107 -174.3112 165.9708 169.3241 152.4023 169.0777 NaN NaN NaN
-170.2709 174.9133 178.0332 169.5016 150.9017 169.8905 NaN NaN NaN
179.7075 174.8766 168.3010 150.7951 143.1325 NaN NaN NaN NaN
165.8388 165.0565 162.8236 140.7707 NaN NaN NaN NaN NaN
152.2543 147.3183 -169.1261 NaN NaN NaN NaN NaN NaN
133.4060 131.5305 NaN NaN NaN NaN NaN NaN NaN]
  2 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 8 Sep 2012
your vector tm contains many values, what do you mean by
if tm<0
do you mean all values<0?
Millo Marin
Millo Marin on 11 Sep 2012
Yes, because for some reason (which I don't know) matlab gives negative degrees in angles that are > 180°, and positive in angles < 180°. Thank you for your reply!

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 8 Sep 2012
Edited: Matt Fig on 8 Sep 2012
for ii=1:numel(tm)
if tm(ii) < 0 % Need to look at each element one-at-a-time!
tm(ii) = 90-tm(ii);
elseif tm(ii) > 90
tm(ii) = 450-(tm(ii));
else
tm(ii) = abs(tm(ii)-90);
end
end
I encourage you to read that article by Loren, and click on the comments she refers to in the first sentence.
  1 Comment
Millo Marin
Millo Marin on 11 Sep 2012
Thanks a lot, it worked perfectly! Again, thanks for take the time to help.

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 8 Sep 2012
Edited: Azzi Abdelmalek on 8 Sep 2012
% maybe is that what you need
for k=1:length(tm)
if tm(k)< 0
tm(k) = abs(tm(k))+90;
elseif tm(k) > 90
tm(k)=360+90-tm(k);
else
tm(k)=abs(tm(k)-90);
end
end
  1 Comment
Millo Marin
Millo Marin on 11 Sep 2012
That was the idea, except that length function gives you the number or columns in a matrix, and I needed to loop for each of the values in the matrix. Thank you for your reply!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!