Animating a complicated 3d graph

26 views (last 30 days)
Harry
Harry on 13 Jul 2020
Edited: jonas on 13 Jul 2020
Hi
I have some complicated data that i would like to visualise via a 3d animation. Here is a picture of the unanimated version of it:
What I'd like to do, is to animate it, so that i can see how the various data points move around, whilst still being able to rotate the graph and look at it from different angles. The barrier is that I am using a number of unusual comands to get this plot and am not sure how to add animation on top of them.
Here is a simplified version of my code:
A = rand(6,5);
%create 3d points to be graphed
startv = [A(:,1),A(:,3),zeros(length(A),1)];
endv = [A(:,2),A(:,5),A(:,4)];
%I want to use the vector endv to determine the colour of the lines/points
%to make the graph clearer; so i am going to create a color vector by
%transforming it
colorv=endv./max(endv);
%graph
figure;hold on
scatter3(startv(:,1),startv(:,2),startv(:,3),5,colorv,'filled');
%scatter3(endv(:,1),endv(:,2),endv(:,3),5,colorv,'filled');
for k = 1 : size(startv, 1)
stem3(endv(k,1),endv(k,2),endv(k,3),...
'LineWidth', 1, 'Color', colorv(k,:), 'MarkerSize', 1)
plot3([startv(k,1)';endv(k,1)'],...
[startv(k,2)';endv(k,2)'],...
[startv(k,3)';endv(k,3)'], '.-',...
'LineWidth', 1, 'Color', colorv(k,:), 'MarkerSize', 1);
end
What I'd like to be able to do would be to use a larger random matrix at the start A=random(6,10) and then have each frame of the animation move from using A(:,5) to A(:,6) to A(:,7) etc but to keep the original colours - so the user can follow individual moving markers.

Answers (1)

jonas
jonas on 13 Jul 2020
Edited: jonas on 13 Jul 2020
I just made some changes to your code to show you a different way of getting a similar result. Basically, just update the ydata for each handle in a for-loop, either by looping over the handles or by doing it in one line (what I've done here). I've added another point to your lines to get rid of the stem plot.
n = 6;
A = rand(n,15);
%create 3d points to be graphed
[x,y,z] = deal([A(:,1),A(:,2),A(:,2)],...
[A(:,3),A(:,5),A(:,5)],...
[zeros(n,1),A(:,4),zeros(n,1)]);
%values to update with later
y_new = A(:,6:end);
%graph
figure;
axis([0,1,0,1,0,1]);hold on
view(3);
grid on;
box on;
h = plot3(x',y',z', '.-',...
'LineWidth', 1, 'MarkerSize', 10);
%if you want to define custom colors, define n-by-3 matrix "colorv", e.g.
%colorv = rand(n,3);
%set(h,{'Color'},mat2cell(colorv,ones(1,n),3))
for i = 1:size(y_new,2)
y_new_c = mat2cell([y(:,1),y_new(:,i),y_new(:,i)],repmat(1,1,n),3);
set(h,{'ydata'},y_new_c)
pause(1)
end

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!