Update a Line Array in one call?
8 views (last 30 days)
Show older comments
Thomas Marullo
on 15 Feb 2019
Commented: Thomas Marullo
on 15 Feb 2019
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 Comments
Accepted Answer
Walter Roberson
on 15 Feb 2019
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 Comments
Walter Roberson
on 15 Feb 2019
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)
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!