How can I let my object repeat over time when animating it?

2 views (last 30 days)
Hello, I am trying to animate a 3d object with the information from the arduino serial port, but the object only appears in another position and the past is not removed, just like this:
Any idea how to make only the figure move without it reappearing? here is the code
clc
for i = 1:20
delete(instrfind({"Port"},{"COM6"}));
micro=serial("COM6");
micro.BaudRate=9600;
warning("off","MATLAB:serial:fscanf:unsuccesfulRead");
fopen(micro)
savedData = fscanf(micro,"%s");
v = strsplit(savedData, ',');
ra = str2double(v(7));
pa= str2double(v(6));
ya= str2double(v(1));
offset_3d_model=[0, 0, 0];
sb= "F22jet.stl";
[Model3D. rb.stl_data.vertices, Model3D.rb.stl_data.faces,~,~]= stlRead(sb);
Model3D.rb.stl_data.vertices= Model3D.rb.stl_data.vertices-offset_3d_model;
AC_DIMENSION = max(max(sqrt(sum(Model3D.rb.stl_data.vertices.^2,2)))) ;
AX=axes("position",[0.0 0.0 1 1]);
axis off
scrsz = get(0,"ScreenSize");
set(gcf,"Position",[scrsz(3)/40 scrsz(4)/12 scrsz(3)/2*1.0 scrsz(3)/2.2*1.0], "Visible","on");
set(AX,"color","none");
axis("equal")
hold on;
cameratoolbar("Show")
AV_hg = hgtransform("Parent",AX,"tag","ACRigidBody");
for j=1:length(Model3D.rb)
AV = patch(Model3D.rb(j).stl_data, "FaceColor", [0 0 1], ...
"EdgeColor", "none", ...
"FaceLighting", "gouraud", ...
"AmbientStrength", 0.15, ...
"Parent", AV_hg);
end
axis("equal");
axis([-1 1 -1 1 -1 1] * 1.0 * AC_DIMENSION)
set(gcf,"Color",[1 1 1])
axis off
view([30 10])
camlight("left");
material("dull");
M=makehgtform("xrotate",ra);
M2=makehgtform("yrotate",pa);
set (AV_hg, 'Matrix', M);
set (AV_hg, 'Matrix', M);
drawnow
delete(micro);
end

Answers (1)

Simon Chan
Simon Chan on 7 Jun 2022
Try to preallocate 'AV' before the for loop and change the data each time inside the for loop:
AV = patch('Faces',1,'Vertices',[NaN NaN], "FaceColor", [0 0 1], ...
"EdgeColor", "none", ...
"FaceLighting", "gouraud", ...
"AmbientStrength", 0.15, ...
"Parent", AV_hg);
%
for j=1:length(Model3D.rb)
set(AV,'Faces', Model3D.rb.stl_data.faces,'Vertices',Model3D.rb.stl_data.vertices)
end

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!