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

compassdegreestruncate

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

Best Answer

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