Consider the following simple example:
pt = [0 0 0];
dir = [1 0 0 1];
h = quiver3(pt(1),pt(2),pt(3), dir(1),dir(2),dir(3));
xlim([-1 1])
ylim([-1 1])
zlim([-1 1])
If we look at h, we'll see that it has properties named UData, VData, and WData.
h =
Quiver with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
XData: 0
YData: 0
ZData: 0
UData: 1
VData: 0
WData: 0
This are the components of the vector. To change the direction, we want to transform those. The makehgtform command will give us a transform matrix for a set of rotations. We can use that to calculate new values for the UData, VData, and WData properties. We can use it like this:
xfm = makehgtform('xrotate',pi/3,'yrotate',pi/5,'zrotate',pi/2);
newdir = xfm * dir';
h.UData = newdir(1);
h.VData = newdir(2);
h.WData = newdir(3);
You may have noticed something odd here. The vector dir is a 1x4 vector even though we've only got 3 directions. The reason is that makehgtform returns a 4x4 matrix. It does that so that it can support translations. You don't care about translations in this case, so we can ignore that.
To animate this, we'd do it in a loop and add a drawnow:
for theta = linspace(0,pi,64)
for phi = linspace(-pi,pi,64)
for psi = linspace(0,2*pi,64)
xfm = makehgtform('xrotate',theta,'yrotate',phi,'zrotate',psi);
newdir = xfm * dir';
h.UData = newdir(1);
h.VData = newdir(2);
h.WData = newdir(3);
drawnow
end
end
end
You may need to fiddle with the order in which you give the rotations to makehgtform. You'll see various orderings referred to as "Euler angles". The help for makehgtform will show you some other things you can do with it, such as the axisrotate option.