Update a Line Array in one call?

조회 수: 13 (최근 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 CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by