Move Group of Objects Along Line
This example shows how to move a group of objects together along a line using transforms.
Plot a sine wave and set the axis limits mode to manual to avoid recalculating the limits during the animation loop.
x = linspace(-6,6,1000); y = sin(x); plot(x,y) axismanual
Create a transform object and set its parent to the current axes. Plot a marker and a text annotation at the beginning of the line. Use thenum2str
function to convert they-value at that point to text. Group the two objects by setting their parents to the transform object.
ax = gca; h = hgtransform('Parent',ax); holdonplot(x(1),y(1),'o','Parent',h); holdofft = text(x(1),y(1),num2str(y(1)),'Parent',h,...'VerticalAlignment','top','FontSize',14);
Move the marker and text to each subsequent point along the line by updating theMatrix
property of the transform object. Use the x and y values of the next point in the line and the first point in the line to determine the transform matrix. Update the text to match they-value as it moves along the line. Usedrawnow
to display the updates to the screen after each iteration.
fork = 2:length(x) m = makehgtform('translate',x(k)-x(1),y(k)-y(1),0); h.Matrix = m; t.String = num2str(y(k)); drawnowend
The animation shows the marker and text moving together along the line.
If you have a lot of data, you can usedrawnow limitrate
instead ofdrawnow
for a faster animation. However,drawnow limitrate
might not draw every update on the screen.
See Also
hgtransform
|makehgtform
|plot
|drawnow
|axis
|text