Update a Line Array in one call?

9 次查看(过去 30 天)
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 中查找有关 Function Creation 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by