フィルターのクリア

Initialize to Increase Efficiency of plot3 Animation in Loop

1 回表示 (過去 30 日間)
Laurel Castillo
Laurel Castillo 2018 年 12 月 18 日
コメント済み: Laurel Castillo 2018 年 12 月 18 日
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.

採用された回答

Steven Lord
Steven Lord 2018 年 12 月 18 日
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 件のコメント
Laurel Castillo
Laurel Castillo 2018 年 12 月 18 日
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 2018 年 12 月 18 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by