# converting atan2 output to 360 deg

120 visualizaciones (últimos 30 días)
shobhit mehrotra el 13 de Abr. de 2015
Comentada: Stephen23 el 13 de Abr. de 2024
Hi, I'm using the function atan2, however my output is from -180 to 180 degrees (I converted from radians) How do I modify it such that it outputs a value from 0-360 degrees?
winddir = (atan2(Vi,Ui))*(180/pi);
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Image Analyst el 13 de Abr. de 2015
There is an atan2d() function you know. All the functions have "d" versions that work in degrees instead of radians. Though it goes from -180 to +180.

Iniciar sesión para comentar.

John D'Errico el 14 de Abr. de 2015
Editada: John D'Errico el 14 de Abr. de 2015
Since it will be periodic, just add 360 if the value is less than 0. This will suffice to correct the negative angles.
winddir = winddir + (winddir < 0)*360;
You can use atan2d if you prefer to work in degrees, but it will map to the same range, [-180,180], so you will still need to correct for the negative angles.
If you wanted a simple expression that works in one line of code, this should do:
winddir = atan2d(Vi,Ui) + 360*(Vi<0);
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
shobhit mehrotra el 14 de Abr. de 2015
most excellent thank you!

Iniciar sesión para comentar.

### Más respuestas (2)

Stephen23 el 30 de Mayo de 2022
Editada: Stephen23 el 30 de Mayo de 2022
The simple, robust, and efficient solution is to use MOD:
mod(atan2d(Y,X),360)
##### 2 comentariosMostrar NingunoOcultar Ninguno
Sam Ruegsegger el 12 de Abr. de 2024
Stephen23,
I am writing to you to thank you for your robust and simple solution. In the midst of a mehatronics lab, trial after trial went by, and my team still could not find a working solution. Despair was creeping in. And then we met you. Your solution has given us a bulwark against the demons of MATLAB. You may have written this 2 years ago, but your legacy lives on in the MathWorks chat room.
Godspeed, brother.
Stephen23 el 13 de Abr. de 2024
@Sam Ruegsegger: you can also vote for answers that helped you. It is an easy way to show your appreciation.

Iniciar sesión para comentar.

theodore panagos el 7 de Nov. de 2018
You can use the formula:
atan(x,y)=180/pi()*(pi()-p()/2*(1+sign(x))*(1-sign(y^2))-pi()/4*(2+sign(x))*sign(y)
-sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y))))
That formula give the angle from 0 to 360 degrees for any value of x and y.
x=x2-x1 and y=y2-y1
For x=y=0 the result is undefined.
##### 2 comentariosMostrar NingunoOcultar Ninguno
theodore panagos el 7 de Nov. de 2018
In the formula put pi()/2 instead p()/2
Nicolas soto vallejos el 30 de Mayo de 2022
Have to say this formula is awsome if you only need to know what quarternal direction the vector is traveling in you could just use the first part. sure only returns in 45 deg but it makes it easier to work with and ofcourse you could allways use the entire thing if you need exact angles but i'm amazed at the brillians of this formula 10/10!

Iniciar sesión para comentar.

### Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by