Create timeseries of a 3d scattered plot

Hello all,
I have a dataset displayed in a 3D scattered plot superimposed with a closed cylinder.
I wish to plot a time series (movie) showing when each scattered plot point was recorded in the scattered plot.
Below is my interial code:
A = 25*rand(8, 5)
A = 8×5
13.0818 5.5140 14.0092 14.8775 8.8787 23.6924 1.5850 15.8762 6.1108 21.2250 19.1817 16.4236 10.1666 17.3767 18.8237 0.4604 13.5761 3.5632 13.5605 2.1653 21.7514 1.2951 20.0804 19.2080 18.5357 5.8638 0.6297 13.3577 8.0470 14.8173 13.1569 8.0734 15.7814 14.1187 24.2023 4.2932 3.9761 21.4168 24.2942 15.3522
Time = A (:,1) ;
x = A (:,2 ) ;
y = A (:,3 ) ;
z = A (:,4 ) ;
Amplitude = A (:,5) ;
scatter3 (x , y, z, 30, Amplitude);
title ('Sample Material')
xlim ([-50.00 50.00])
ylim ([-50.00 50.00])
zlim ([0.00 100.00])
xlabel ('Breath /m')
ylabel ('Width /m')
zlabel ('Height /m')
pbaspect([1 1 2])
H = colorbar ;
ylabel (H, 'Amplitude')
hold on
r = 25.000;
[X,Y,Z] = cylinder(r);
hsurf = surf (X,Y,Z*100, 'EdgeColor', 'none', 'FaceAlpha', 0.2); % capture the handle of the surf object
patch(X(1,:),Y(1,:),Z(1,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
patch(X(1,:),Y(1,:),Z(2,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
My current challenge comes from adding the time series. I will be grateful to have some comments on how to proceed.
Here is my additional code for the timespan:
ts1 = timeseries(A (:,1), x,y,z,Amplitude);
ts1.Name = 'Sample Material - Time Span';
ts1.TimeInfo.Units = 'hours';
ts1.TimeInfo.StartTime = '00.00';
ts1.TimeInfo.Format = 'hh mm, ss';
ts1.Time = ts1.Time - ts1.Time(1);
plot(ts1);
Thank you.

4 Comments

Simply using a for loop and a pause may create a movie-like presentation.
Try the following to see whether this is what you want.
for k = 1:10
A = 25*rand(8, 5);
Time = A (:,1) ;
x = A (:,2 ) ;
y = A (:,3 ) ;
z = A (:,4 ) ;
Amplitude = A (:,5) ;
scatter3 (x , y, z, 30, Amplitude);
ax = gca;
title ('Sample Material')
xlim ([-50.00 50.00])
ylim ([-50.00 50.00])
zlim ([0.00 100.00])
xlabel ('Breath /m')
ylabel ('Width /m')
zlabel ('Height /m')
pbaspect([1 1 2])
H = colorbar ;
ylabel (H, 'Amplitude')
hold(ax,'on')
r = 25.000;
[X,Y,Z] = cylinder(r);
hsurf = surf (X,Y,Z*100, 'EdgeColor', 'none', 'FaceAlpha', 0.2); % capture the handle of the surf object
patch(X(1,:),Y(1,:),Z(1,:), hsurf.CData(1,:), 'FaceAlpha', 0.2);
patch(X(1,:),Y(1,:),Z(2,:), hsurf.CData(1,:), 'FaceAlpha', 0.2);
pause(1);
hold(ax,'off');
end
Hello,
Thank you for your response.
The loop and pause option is helpful but the challenge I have with that is all scattered points are displayed on the graph at the same time. My intention is to display each scattered point on the graph with respect to the time
Time = A (:,1) %in hours
until all points are captured on the same graph.
Will it be possible to execute such an idea?
Thank you.
Modify slightly and let see the following is what you want? You may use this as the starting point.
for k = 1:10
A = 25*rand(8, 5);
Time = A (:,1) ;
x = A (:,2 ) ;
y = A (:,3 ) ;
z = A (:,4 ) ;
Amplitude = A (:,5) ;
scatter3 (x , y, z, 30, Amplitude);
ax = gca;
if k == 1
title ('Sample Material')
xlim ([-50.00 50.00])
ylim ([-50.00 50.00])
zlim ([0.00 100.00])
xlabel ('Breath /m')
ylabel ('Width /m')
zlabel ('Height /m')
pbaspect([1 1 2])
H = colorbar ;
ylabel (H, 'Amplitude')
hold(ax,'on')
r = 25.000;
[X,Y,Z] = cylinder(r);
hsurf = surf (X,Y,Z*100, 'EdgeColor', 'none', 'FaceAlpha', 0.2); % capture the handle of the surf object
patch(X(1,:),Y(1,:),Z(1,:), hsurf.CData(1,:), 'FaceAlpha', 0.2);
patch(X(1,:),Y(1,:),Z(2,:), hsurf.CData(1,:), 'FaceAlpha', 0.2);
else
pause(1);
end
end
Hello,
Thank you for your reply. I tried this option.
Could you kindly tell me what you mean by I may use this as the starting point?
The code works well for my first line of dataset then subsequently not.
The idea is to present the evolution of the scattered data on the same graph with respect to time.
Time = A (:,1) ; %Time in hours from the dataset
Perhaps, I should draft the question better and submit again?
Thank you

Sign in to comment.

 Accepted Answer

If you want to create an animation (GIF for instance) or a movie (e.g. AVI), you can:
% see: https://mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab
for i = 1:20
A = 25*rand(8, 5);
Time = A (:,1) ;
x = A (:,2 ) ;
y = A (:,3 ) ;
z = A (:,4 ) ;
Amplitude = A (:,5) ;
scatter3 (x , y, z, 30, Amplitude);
title ('Sample Material')
xlim ([-50.00 50.00])
ylim ([-50.00 50.00])
zlim ([0.00 100.00])
xlabel ('Breath /m')
ylabel ('Width /m')
zlabel ('Height /m')
pbaspect([1 1 2])
H = colorbar ;
ylabel (H, 'Amplitude')
hold on
r = 25.000;
[X,Y,Z] = cylinder(r);
hsurf = surf (X,Y,Z*100, 'EdgeColor', 'none', 'FaceAlpha', 0.2); % capture the handle of the surf object
patch(X(1,:),Y(1,:),Z(1,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
patch(X(1,:),Y(1,:),Z(2,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
hold off
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind, cm, 'myanimation.gif', 'gif', 'Loopcount', 1);
else
imwrite(imind, cm, 'myanimation.gif', 'gif', 'WriteMode', 'append');
end
end
Similarly, for a movie:
for i = 1:20
% plot
drawnow
f(i) = getframe(gcf);
end
vidobj = VideoWriter('mymovie.avi');
vidobj.FrameRate = 10;
open(vidobj);
for i = 1:numel(f); writeVideo(vidobj, f(i)); end
close(vidobj);

8 Comments

Hello,
Thank you very much for your response and the attached reference. They have been very helpful.
I have tried your examples which work well. I however have a challenge when I try to replace
A = 25*rand(8, 5);
in the code with loading a .txt file
A = load ('sampleMaterial.txt');
Is there a way that I could load a .txt file and display each scattered plot point with respect to time
Time = A (:,1) ; %Time in hours from the dataset
in a gif or video as your examples?
Thank you once again.
can you attach 'sampleMaterial.txt'?
Yes, please find it atatched.
Thank you
Assuming you deleted the first line in the text file:
A = readtable('sampleMaterial.txt');
for i = 1:A.Time
scatter3 (A.x(i) , A.y(i), A.z(i), 30, A.Amplitude(i), 'filled');
title ('Sample Material')
xlim ([-50.00 50.00])
ylim ([-50.00 50.00])
zlim ([0.00 100.00])
xlabel ('Breath /m')
ylabel ('Width /m')
zlabel ('Height /m')
title(compose("Time = %.2f s", A.Time(i)))
pbaspect([1 1 2])
H = colorbar ;
ylabel (H, 'Amplitude')
hold on
r = 25.000;
[X,Y,Z] = cylinder(r);
hsurf = surf (X,Y,Z*100, 'EdgeColor', 'none', 'FaceAlpha', 0.2); % capture the handle of the surf object
patch(X(1,:),Y(1,:),Z(1,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
patch(X(1,:),Y(1,:),Z(2,:), hsurf.CData(1,:), 'FaceAlpha', 0.2)
hold off
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind, cm, 'myanimation.gif', 'gif', 'Loopcount', 1);
else
imwrite(imind, cm, 'myanimation.gif', 'gif', 'WriteMode', 'append');
end
end
Thank you very much for your response. The code works well now.
I am trying to adjust it now to show a cummulation of the points as the time elapsed.
Thank you once again.
--
Greek
Just remove hold off...
Hello,
I eventually figured that out. You've been so helpful.
I am now going to practice with several lines of the same data.
Thank you once more.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 2 Feb 2022

Commented:

on 4 Feb 2022

Community Treasure Hunt

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

Start Hunting!