MATLAB: How can i make a 3d conical helix plot itself slowly

drawnowhelixMATLABplotrealtime

So i have these following lines of code and now all it does is give me the product of the plot right away. However i want to see it being drawn before my eyes! How can this be done? Using drawnow? And if drawnow how do i implemnt it?

Best Answer

Fredrik - you can iterate over each element in x, y, and z and either plot the new points, or just update that which has already been plotted with "a little more" data. So we start off as you have done already with
% close any open figures
close all;
% create the helix data to plot
varv=10;
a = 0.05;
c = 5.0;
t = 0:0.01:varv*2*pi;
x = (a*t/2*pi*c).*sin(t);
y = (a*t/2*pi*c).*cos(t);
z = t/(2*pi*c);
% create a figure
figure(3);
Now, we just create the graphics object that will be used to draw the 3D data, and add labels and a title to the axes.
% create the graphics object
h = plot3(NaN,NaN,NaN);
% label the axes
xlabel('x');
ylabel('y');
zlabel('z');
title('Conical helix');
The call to plot3 with NaN inputs does not plot anything, it just creates the graphics handle whose data we will update. We now set limits on the axes so that when we do draw, the axes limits remain fixed.
% set limits
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
Now we iterate over each point in our data, and update the graphics handle h with the new data, pausing for 1 millisecond to allow the new data to be drawn on the axes.
% plot the data
for k=1:length(x)
set(h,'XData',x(1:k),'YData',y(1:k),'ZData',z(1:k));
pause(0.001);
end
As you can see in the above code, on each iteration of the for loop we update the XData, YData and ZData with a new point.
Try running the above and see what happens!
Related Question