title: Calculate heading angle from x and y information
body: <p>I have data that records the x and y positions of an animal in a 2D assay over time stored in a matlab matrix. I can plot these co-ordinates over time, and extract the velocity information and plot this using cline.</p>
  
  <p>The problem I am having at the moment is calculating the heading angle. It should be a trivial trigonometry question, but I am drawing a blank on the best way to start. </p>
  
  <p>The data is stored in a matrix xy representing x and y co-ordinates:</p>
  
  <pre><code>    796.995391705069    151.755760368664
      794.490825688073    150.036697247706
      788.098591549296    145.854460093897
      786.617021276596    144.327659574468
      781.125000000000    140.093750000000
      779.297872340426    138.072340425532
      775.294642857143    133.879464285714
  </code></pre>
  
  <p>What I would like to be able to do is know the angle of the line drawn from (796.995, 151.755) to (794.490, 150.036), and so on. My research suggests atan2 will be the appropriate function, but I am unsure how to call it correctly to give useful information.</p>
  
  <pre><code>    difx = xy(1,1) - xy(2,1);
      dify = xy(1,2) - xy(2,2);
      angle = atan2(dify,difx);
      angle = angle*180/pi % convert to degrees
  </code></pre>
  
  <p>The result is 34.4646. Is this correct?</p>
  
  <p>If it is correct, how do I get the value to be in the range 0-360?</p>