add points to plot

6 views (last 30 days)
Nuno
Nuno on 4 Mar 2011
I'm using the following code to add points to a plot the fastest way possible. But this way I need to read the existing points, add the news and set them back. Is there a simpler way?
if ishandle(handle_in)
set(handle_in,'XData',[get(handle_in,'XData') posicaoX],'YData',[get(handle_in,'YData') posicaoY]);
handle_out=handle_in;
else handle_out=plot(posicaoX,posicaoY,options,'LineWidth',1,'MarkerSize',10);
end
  1 Comment
Andrew Newell
Andrew Newell on 4 Mar 2011
Could you please format this code so that it is more readable? See http://www.mathworks.com/matlabcentral/answers/help/markup.

Sign in to comment.

Accepted Answer

Jan
Jan on 4 Mar 2011
Your soultion looks efficient. The distinction between "handle_in" and "handle_out" is not needed and the line "handle_out=handle_in" can be omitted if you use LineH for both (not "handle", because this is used by Matlab already).
One idea would be to create a LINE without data at first, such that the ISHANDLE test is not necessary.
If you update the line very often (>10.000 times), pre-allocation will increase the speed: Initialize the line with the maximal number of elements and set them to Inf. Store the current index for inserting new values e.g. in the UserData of the line.
  6 Comments
Jan
Jan on 4 Mar 2011
Another method to gain speed: The XData and YData are searched at each update for min and max values to update the X- and Y-limits. If you can fix these limits (e.g. 'XLim', [0,10000], 'YLim', [-1, 1]) and set the undocumented properties 'YLimInclude' and 'XLimInclude' of the LINE handle to 'off', Matlab does not search the new min and max.
Jan
Jan on 4 Mar 2011
@Walter: Inf's and NaN's are both not drawn.
@Nuno: If you have fixed size for X-limits and Y-limits, I could summarize the ideas in a new code example.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!