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)
0 个评论
采纳的回答
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
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!