How can I orbit a plot3 via mouse while updating it?

5 views (last 30 days)
I have a plot that I want to be able to orbit with my mouse while it's updating. But every update to the plot forces the plot to go back to it's default view point. Is there a way to do what I want?
Thanks in advance!

Accepted Answer

Geoff Hayes
Geoff Hayes on 12 Sep 2014
Edited: Geoff Hayes on 12 Sep 2014
Dereck - how is your code updating the figure at each iteration? Is it re-plotting all 3D data with the addition of some new points, or are you just updating the axes with the new points?
I used the helix example from plot3 and noticed that if I plotted the data, then rotated the axes, and then plotted the data again, that the view point of the axes was restored to its original value. However, if after I rotated the axes and then called the command
hold on
for the current axes, that the subsequent call to plot the same data again, resulted in the view point of the axes remaining fixed at what I rotated it to. (For details on hold on, type doc hold - basically it retains the current graph and axes properties when adding a new graph/plot, rather than resetting/clearing the axes properties.)
So you could do the same here - call hold on just once to ensure that the axes properties (in this case View which returns the azimuth and elevation of the graph) are maintained from one call of plot3 to another. Or, just after creating the figure, you could try
set(gca,'NextPlot','add');
Which will set the current axes (its handle obtained from gca, get current axes) NextPlot property to add which will use the existing axes to draw the graph (the default is replace which resets all axes properties). See axes properties for details. This method worked well - I could rotate the figure and re-plot the graph without loss to my last view point of the axes.

More Answers (1)

Dereck
Dereck on 12 Sep 2014
Edited: Dereck on 12 Sep 2014
I found a solution that worked better for me based on Geoff's Answer:
figure(1)
clf;
for i = 1:100000
set(0, 'CurrentFigure', 1);
h = plot3(X(i,:), Y(i,:), Z(i,:),);
drawnow();
pause(0.1);
delete(h);
end
The key was to remove the clf from the loop by deleting only the plot data rather than the whole plot.
Also, I had to remove the figure() form the loop to prevent it from switching between other figures while orbiting.
  2 Comments
Dereck
Dereck on 12 Sep 2014
Bah, this only worked for a short time. Then matlab started slowing down as if I was not actually deleting the points.
Geoff Hayes
Geoff Hayes on 13 Sep 2014
Strange how it slowed down over time. Was it necessary for the set(0, 'CurrentFigure', 1); inside the for loop so that it would repeat again and again?
An alternative to the deleting of the handle is to (perhaps) just replace the data at each iteration
figure(1);
% plot the first set of data outside of loop
h = plot3(X(1,:), Y(1,:), Z(1,:));
for k=2:100000
set(h,'XData',X(k,:),'YData',Y(k,:),'ZData',Z(k,:));
drawnow;
pause(0.1);
end
So we plot the first set (k==1) initially so that we can get the handle to the 3D plot graphics object. And then on each subsequent iteration of the for loop, we just replace the X,Y, and Z data.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!