Euler Angles to Quaternion transformation and back (independent of rotation order)

Here is a bit of matlab code to illustrate how you can convert Euler angles to a Quaternion. The function outputs resulting rotation in Axis Angle form and also prints Euler Angles (transformed from Quaternion) just to show that transformation is correct.

If you are interested in how to transform Quaternion to Euler Angles, have a look at functions to_euler and get_p.