add points to plot

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

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

2 votes

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

If you initialize the line with nan instead of inf, then Matlab will not attempt to draw those positions.
Nuno
Nuno on 4 Mar 2011
Thanks. handle_in and handle_out were input and output parameters. I now checked I can have them both with the same name on the function.
While pre-alocating saves the array expansion time, it also introduces more time calculating the plot, when it was suppose to be small.
That is the problem, I don't know any inexpensive solution of changing/adding a point on a 100.000 points plot.
Nuno
Nuno on 4 Mar 2011
Walter: I've only seen your answer after commenting. So it allows the pre-allocation to save some time.
But it's still very expensive to change one point if I have A LOT of others set.
Nuno
Nuno on 4 Mar 2011
Walter: Actually drawing inf is faster than NaN.
I'm writing like this:
A_XData=inf*ones(1000,1);
A_YData=inf*ones(1000,1);
A_XData(1)=10;
A_YData(1)=50;
plot(A_XData,A_YData,'r','LineWidth',1);
B_XData=nan*ones(1000,1);
B_YData=nan*ones(1000,1);
B_XData(1)=10;
B_YData(1)=50;
plot(B_XData,B_YData,'r','LineWidth',1);
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)

Asked:

on 4 Mar 2011

Community Treasure Hunt

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

Start Hunting!