フィルターのクリア

Update a Line Array in one call?

7 ビュー (過去 30 日間)
Thomas Marullo
Thomas Marullo 2019 年 2 月 15 日
コメント済み: Thomas Marullo 2019 年 2 月 15 日
Is it possible to update a line array in one shot instead of using a for loop? Here is example code. You can see I need to perform a for loop over the line array to update each X Y and Z coordinate pairs. I can't seem to figure out a way to do this in one shot to hopefully improve the speed. My end goal is to have this animation draw as quick as possible.
%% 2000 line figure, random for example
X = rand(2,2000);
Y = rand(2,2000);
Z = rand(2,2000);
figure;
view(45,45);
xlabel('X-dimen');
ylabel('Y-dimen');
zlabel('Z-dimen');
drawlines_time = tic;
% Initial line draw
AllLines = line (X,Y,Z, 'Color','b','LineWidth',[0.5]);
t = toc(drawlines_time);
fprintf('Time to draw lines %2.4f sec\n',t);
%% Update coordinates and redraw
X = rand(2,2000);
Y = rand(2,2000);
Z = rand(2,2000);
redrawlines_time = tic;
% For each line pair update the coordinates this is time consuming
for ii = 1:numel(AllLines)
AllLines(ii).XData = X(:,ii)';
AllLines(ii).YData = Y(:,ii)';
AllLines(ii).ZData = Z(:,ii)';
end
t = toc(redrawlines_time);
fprintf('Time to update lines %2.4f sec\n',t);
Output on my end:
Time to draw lines 0.8088 sec
Time to update lines 0.2910 sec
Significantly faster to update the handles, but I was hoping to improve the performance with maybe doing a "AllLines(:).XData = X" type function (which doesn't work)

採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 15 日
Xc = num2cell(X,1);
Yc = num2cell(Y,1);
Zc = num2cell(Z,1);
set(AllLines, {'XData','YData','ZData'}, [Xc.', Yc.', Zc.'])
setting 3 properties for each of 2000 handles requires a 2000 x 3 cell array, each entry of which is the new value (which in this case is a vector of length 2.)
I do not know how this is implemented internally so you would want to time it.
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 2 月 15 日
You also might want to try
set(AllLines, {'XData', 'YData', 'ZData'}, permute(num2cell(cat(3,X,Y,Z),1),[2 3 1]))
it might be marginally faster (probably not much)
Thomas Marullo
Thomas Marullo 2019 年 2 月 15 日
Thanks, but I see no difference in timing.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by