How do I create a time-lapse video of a 3D plot?

20 views (last 30 days)
I have a recording of the trajectory of the two feet of a person, given as two time series of 3D coordinates. (two matrices of Nx3, with N being the number of samples). The feet are modeled as points in 3D space. I want to create a video that shows the movement of the feet over time.
What would be the best way of doing this in MATLAB?

Accepted Answer

Kevin Holly
Kevin Holly on 28 Jun 2023
Edited: Kevin Holly on 28 Jun 2023
One way is to run a similar code as shown below in Live Editor. It will automatically create a video from the for loop that you can export.
matrix = rand(10,3);
matrix2 = rand(10,3);
ii=1;
foot1 = scatter3(matrix(ii,1),matrix(ii,2),matrix(ii,3),'filled','r');
hold on
foot2 = scatter3(matrix2(ii,1),matrix2(ii,2),matrix2(ii,3),'filled','g');
xlim([0 1])
ylim([0 1])
zlim([0 1])
for ii = 2:size(matrix,1)
foot1.XData = matrix(ii,1);
foot1.YData = matrix(ii,2);
foot1.ZData = matrix(ii,3);
foot2.XData = matrix2(ii,1);
foot2.YData = matrix2(ii,2);
foot2.ZData = matrix2(ii,3);
drawnow
end
Or you can use writeVideo and getframe function
v = VideoWriter('NameYourVideo.avi');
v.FrameRate = 1;
open(v);
for ii = 2:size(matrix,1)
foot1.XData = matrix(ii,1);
foot1.YData = matrix(ii,2);
foot1.ZData = matrix(ii,3);
foot2.XData = matrix2(ii,1);
foot2.YData = matrix2(ii,2);
foot2.ZData = matrix2(ii,3);
drawnow
frame = getframe(gca); %Note if you use gca (get current axis), make sure it doesn't change size - x, y, and z limits were defined above in this example to prevent this.
writeVideo(v,frame);
end
close(v)
  3 Comments
Kevin Holly
Kevin Holly on 29 Jun 2023
If it worked, I would appreciate it if you would accept the answer.
Etay Kalifa
Etay Kalifa on 29 Jun 2023
would you happen to know how I could add a time stamp to the video based on the sample rate?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!