Display a degree heading as a compass heading rather than a absolute heading from the origin.

9 views (last 30 days)
title(sprintf('HMNZS AOTEAROA Ship motion in Sea state 6,\n Heading: %3.2f%c Runtime:%4.2f sec'...
, d , char(176), t), "Color", [1 1 1]); % Display Graph title with changeing data
I am wanting to display the heading of the vessel in a compass format. Currently, as it goes around in a circle the heading goes up to 758 degrees I believe there is a way to make it only display a heading from 0 to 359 deg.
I think it goes like this
C = Heading / 360
H = ??? % truncate CH to only leave digits after the decimal point
CH = H*360
Not sure how to truncate the whole numbers.
Thanks in advance
  2 Comments
Strahan Kelly
Strahan Kelly on 2 Feb 2021
I believe I found the answer the function I was after is rem(a,b).
CH= rem(Heading,360) % This will give me the remainder of the degrees
% that are not part of a complete loop/ circle
So if Heading =760
Ch = 40
Adam Danz
Adam Danz on 2 Feb 2021
Edited: Adam Danz on 2 Feb 2021
mod is better than rem for reasons explained in my answer below. Also, you should decide what should happen when Heading is 360: should it convert to 0 or stay at 360? That's also explained in my answer.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 2 Feb 2021
Edited: Adam Danz on 2 Feb 2021
See wrapTo360() from the Mapping Toolbox.
Otherwise,
Use mod() to wrap a value to [0,360] rather than rem()
theta = 758;
wrapped = mod(theta, 360);
Examples
mod(540, 360)
ans = 180
mod(-90, 360)
ans = 270
mod(360,360) % Note: returns 0
ans = 0
mod(360*2, 360) % Note: Returns 0
ans = 0
To keep 360 from changing to 0,
theta = 360;
mod(0, 360) + 360*(mod(theta, 360)==0 && theta>0)
ans = 360
theta = 360*2
theta = 720
mod(0, 360) + 360*(mod(theta, 360)==0 && theta>0)
ans = 360
Look what happens when you use rem
rem(-90,360) % oops!
ans = -90

More Answers (0)

Categories

Find more on 3-D Scene Control in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!