Filter löschen
Filter löschen

Initialize to Increase Efficiency of plot3 Animation in Loop

1 Ansicht (letzte 30 Tage)
plot.PNG
One line is stationary. The moving line is drawn with the psm_xp1V set (4*4*10). In each loop, a set of 4*4 matrix is used to update the values of the line.
Here is my main. Basically I just load the data and put the animiation function in the loop.
psm_xp1V_data = load ('psm_xp1V.mat', 'psm_xp1V');
for i=1:10
psm_xp1V=psm_xp1V_data.psm_xp1V(:,:,i);
[frame_plot] = animation3(psm_xp1V);
end
The animatioin function. The Base line is constant. And the Tip line updates in each loop, according to the dataset.
function [frame_plot]=animation3(psm_xp1V)
figure(3); % BTW I added these two lines here to make the figure as an output of the function. Do I really have to do so?
frame_plot = figure(3); %
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
grid on;
view(3);
s=0.1;
cla;
%Base
Base = eye(4);
BaseO = Base(1:3,4);
BaseX = Base(1:3,1).*s;
plot3([BaseO(1) BaseX(1)+BaseO(1)], [BaseO(2) BaseX(2)+BaseO(2)], [BaseO(3) BaseX(3)+BaseO(3)], ...
'color', 'r', 'linewidth', 3);
hold on % And do I really need hold on?
%Tip
tip_x = psm_xp1V; %input
TipO = tip_x(1:3,4);
TipX = tip_x(1:3,1).*s;
plot3([TipO(1) TipX(1)+TipO(1)], [TipO(2) TipX(2)+TipO(2)], [TipO(3) TipX(3)+TipO(3)], ...
'color', 'r', 'linewidth', 3);
hold on
drawnow;
end
Can I increase the efficiency by adding another function to initialize the figure before the loop?
So we don't have to create and update the whole figure in each loop. Maybe just update the values for the moving line Tip in each loop.
Please advise.

Akzeptierte Antwort

Steven Lord
Steven Lord am 18 Dez. 2018
Don't call plot3 over and over, which will keep increasing the number of graphics objects present in the figure. Instead either use the animatedline function or use one of the techniques described in the "Animation Techniques" section on this documentation page.
  3 Kommentare
Laurel Castillo
Laurel Castillo am 18 Dez. 2018
An update!
I managed to modify it into this, extracting the stationary parts out of the loop.
But I am not able to turn the plotting part in the loop to a function.......
psm_xp1V_data = load ('psm_xp1V.mat', 'psm_xp1V');
s=0.1;
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
grid on;
view(3);
Base = eye(4);
BaseO = Base(1:3,4);
BaseX = Base(1:3,1).*s;
bx = animatedline([BaseO(1) BaseX(1)+BaseO(1)],[BaseO(2) BaseX(2)+BaseO(2)],[BaseO(3) BaseX(3)+BaseO(3)], 'Color', 'r','linewidth', 3);
tx = animatedline('MaximumNumPoints',2,'Color', 'r','linewidth', 3);
for i=1:10
psm_xp1V=psm_xp1V_data.psm_xp1V(:,:,i);
% cla;
TipO = psm_xp1V(1:3,4);
TipX = psm_xp1V(1:3,1).*s;
addpoints(tx,[TipO(1) TipX(1)+TipO(1)],[TipO(2) TipX(2)+TipO(2)],[TipO(3) TipX(3)+TipO(3)]);
drawnow;
end
Laurel Castillo
Laurel Castillo am 18 Dez. 2018
OK! Turned the animation part into a function! Any advise to speed it up further?
psm_xp1V_data = load ('psm_xp1V.mat', 'psm_xp1V');
s=0.1;
axis([-0.5 0.5 -0.5 0.5 -0.5 0.5]);
grid on;
view(3);
Base = eye(4);
BaseO = Base(1:3,4);
BaseX = Base(1:3,1).*s;
bx = animatedline([BaseO(1) BaseX(1)+BaseO(1)],[BaseO(2) BaseX(2)+BaseO(2)],[BaseO(3) BaseX(3)+BaseO(3)], 'Color', 'r','linewidth', 3);
tx = animatedline('MaximumNumPoints',2,'Color', 'r','linewidth', 3);
for i=1:10
psm_xp1V=psm_xp1V_data.psm_xp1V(:,:,i);
animation4(tx, psm_xp1V);
end
function animation4(tx, psm_xp1V)
s=0.1;
TipO = psm_xp1V(1:3,4);
TipX = psm_xp1V(1:3,1).*s;
addpoints(tx,[TipO(1) TipX(1)+TipO(1)],[TipO(2) TipX(2)+TipO(2)],[TipO(3) TipX(3)+TipO(3)]);
drawnow;
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Animation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by