Hello and thank you in advance for your help!
I made a 3D scatter plot with 4 color groups using MATLAB (uploaded, see below ).
Now I want to animate the scatter plot with respect to time. So if each point has a time stamp I would like to show them in order.
So for example: if I have points A, B, C that repent errors on a specific xyz location of a car, and error A was made at 10AM and error B at 12PM and error C at 3PM I want to plot the points in that order in an animation.
Also if possible I would like to make a GUI with a scrolling bar so I can scroll trough time or back in time and thus adding points as I go forward into time or removing points as I go back in time. or at least an option to pause the scater process.
Note: the scatter plot is going to have about 2000-3000 points … I don’t know if this will make a difference. Im also new to MATLAB :-)
Thank you very much for your help and time!
x = [ 50 55 200 210 350 360 400 450 550 560 600 670 750 850 860];
y = [ 50 -50 100 -100 150 -150 151 -151 150 -150 152 -152 150 -150 150];
z = [ 120 120 100 300 100 300 100 300 100 300 100 300 100 300 100];
% alocates space for the z data by creating a matrix array of all ones
g = [0*ones(3,1);1*ones(3,1); 2*ones(3,1); 3*ones(3,1); 4*ones(3,1); ];
%set specific RGB color value for positions 0-4 and background color
color = [0 0 0; 1 0 0; 0 0 1; 1 1 0; 0 1 0]
whitebg([ 0.6758 0.8438 0.8984]) % light blue background
% gscatter creates a 2D matrix with the values from x and y
% and creates groups acording to the 'g' matrix size
% h = gscatter captures output argument and returns an array of handles to the lines on the graph)
h = gscatter(x, y, g, color)
%% for each unique group in 'g', set the ZData property appropriately
gu = unique(g);
for k = 1:numel(gu)
set(h(k), 'ZData', z( g == gu(k) ));
%set the aspect ratio, grid lines, and Legend names for the 3D figure
daspect([4.5 5 5])
legend('Position 0','Position 1','Position 2','Position 3','Position 4')
% view a 3D grapgh (for 2D set to "2")
If I understand you correctly, you just want to show all the different scatter plots after one another. That is easy enough; just append this to the code you have:
% loop through time xl = xlim; yl = ylim; zl = zlim; for ii = h(:).' % switch all scatter plots off set(h, 'visible', 'off') % switch only 1 on, for the current time set(ii, 'visible', 'on'); % set original limits xlim(xl); ylim(yl); zlim(zl); % draw and some delay drawnow, pause(1); end
As for the slider, here's an example of how to do that. Append this to the code you have:
xl = xlim; yl = ylim; zl = zlim; slider = uicontrol(... 'parent', gcf,... 'style', 'slider',... 'min', 0,... 'max', 4,... 'sliderstep', [1/4 1/4],... 'units', 'normalized',... 'position', [0.05 0.05 0.90 0.05],... 'callback', @SliderCallback); function SliderCallback(sliderObj, ~) set(h, 'visible', 'off') set(h(get(sliderObj, 'Value')+1), 'visible', 'on') xlim(xl); ylim(yl); zlim(zl); end
Note that this uses a nested
function, which implies your entire script needs to become a function (which is really the proper way to do things of any appreciable size anyway).