Plot 3D Animation
19 ビュー (過去 30 日間)
古いコメントを表示
Good morning,
I am currently developing my master thesis in MATLAB, developing an academic Multibody Systems algorithm. I am currently having some difficulties doing my debug and I was trying to code a 3D Animated Plot to try and understand what was my system behaviour.
This is my current code:
%% 3D Animation Plot
if strcmp(anigraph,'yes') == 1 || strcmp(anigraph,'YES') == 1 || strcmp(anigraph,'Yes') == 1
NPoint = Joints.NPoint; %Number of Points
origin = [0,0,0]; %Origin
%% Plot the Graphic
for f = 1:1667:c %Size of the Point StructStruct
%Plot First two Points of each iteration
strucPos = Points{1,f};
vecpos = strucPos(2).Position;
vecpos = vecpos';
%Initiate the 3D Plot
m = plot3([origin(1) vecpos(1)],[origin(2) vecpos(2)],[origin(3) vecpos(3)],'LineWidth',2);
hold on
axis auto
grid on
%Retrieve the Position Information and plot
for h = 2:NPoint
% Previous Point Pos Vector
prevecpos = strucPos(h-1).Position;
prevecpos = Impose_Column(prevecpos);
prevecpos = prevecpos';
% Foward Point Pos Vector
vecpos = strucPos(h).Position;
vecpos = Impose_Column(vecpos);
vecpos = vecpos';
%Plot These Points
plot3([prevecpos(1) vecpos(1)],[prevecpos(2) vecpos(2)],[prevecpos(3) vecpos(3)],'LineWidth',2),
hold on
pause(TimeStep);
end
end
hold off
end
I followed this code and video: Youtube
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1048565/image.png)
All my data for the plot is stored in a cell with multiple structs. Here is the example of one of these Structs, I desire to use only the data from the first column.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1048570/image.png)
I have been trying to plot something like this for every iteration:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1048575/image.png)
However, I am only being able to plot the first vector, can someone help me understand what I am missing on my code? My main difficulty is that the number of points change with the system that I try to simulate.
Thank you for all your help and attention.
Tiago
EDIT: Added a new code that is now able to plot the first iteration atleast.
0 件のコメント
採用された回答
Karim
2022 年 6 月 28 日
not sure witouth some example data, could you share for instance the struct holding the time history of the solution?
As a tip, I would only update the plot once at each time step. You can use plot3 to plot multiple vectors at once, just seperate them with nan's. I will add a small example below.
MyGrid = [ -0.5 0 0; 0.5 0 0; 0 0 0; 0 -2 0; -1.5 -1.5 0; 1.5 -1.5 0];
MyPoints = [1 2; 3 4; 1 5; 2 6]; % column 1 gives the start point of each vector, column 2 the stop point
figure
plot3( reshape([MyGrid(MyPoints(:,1),1)';MyGrid(MyPoints(:,2),1)';NaN(1,numel(MyPoints(:,1)))],1,[]),...
reshape([MyGrid(MyPoints(:,1),2)';MyGrid(MyPoints(:,2),2)';NaN(1,numel(MyPoints(:,1)))],1,[]),...
reshape([MyGrid(MyPoints(:,1),3)';MyGrid(MyPoints(:,2),3)';NaN(1,numel(MyPoints(:,1)))],1,[]),'r')
grid on
view([0 90])
% now plot the figure again and rotate it
figure
m = plot3( reshape([MyGrid(MyPoints(:,1),1)';MyGrid(MyPoints(:,2),1)';NaN(1,numel(MyPoints(:,1)))],1,[]),...
reshape([MyGrid(MyPoints(:,1),2)';MyGrid(MyPoints(:,2),2)';NaN(1,numel(MyPoints(:,1)))],1,[]),...
reshape([MyGrid(MyPoints(:,1),3)';MyGrid(MyPoints(:,2),3)';NaN(1,numel(MyPoints(:,1)))],1,[]),'r');
grid on
view([0 90])
% to update the figure, we can use "m" directly, say we want to rotate the
% grid by 30 degrees:
RotGrid = (rotz(30) * MyGrid')';
% note you can put this in a simple loop
m.XData = reshape([RotGrid(MyPoints(:,1),1)';RotGrid(MyPoints(:,2),1)';NaN(1,numel(MyPoints(:,1)))],1,[]);
m.YData = reshape([RotGrid(MyPoints(:,1),2)';RotGrid(MyPoints(:,2),2)';NaN(1,numel(MyPoints(:,1)))],1,[]);
m.ZData = reshape([RotGrid(MyPoints(:,1),3)';RotGrid(MyPoints(:,2),3)';NaN(1,numel(MyPoints(:,1)))],1,[]);
4 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!