How to retrieve multiple handles from plot function?

Hello,
I'm trying to plot some points in a figure and need to change the color of the points during the different iterations of the algorithm.
My points are stored in a two-dimensional vector, when I plot it the following way, everything works as expected: handle = plot( p(1 ,1), p(1 ,2), 'o', ..., p(n,1), p(n,2), 'o');
I can then set the color of the point by using: set(handle(1) ,'MarkerEdgeColor','r','MarkerFaceColor','r'); and so on...
This is working as expected, I can change the color of each individual point, so some points can be red others green and so on.
My problem is, the number of points are not always the same. So instead of using the plot command with every point I tried to use: handle = plot( p(:,1), p(:,2), 'o');
However, this way only one handle is returned, all points will have the same color and I can't change the color later on in the code.
Is there any way to plot a unknown number of points as above and still get handles which I can use to set color?
I'm using Matlab 2012a.
Best Regards, Stephan

 Respuesta aceptada

Kelly Kearney
Kelly Kearney el 27 de Mayo de 2013
A few options. You could write a loop...
for ii = 1:size(p,1)
h(ii) = plot(p(ii,1), p(ii,2), 'o');
end
or use arrayfun
h = arrayfun(@(x,y) plot(x,y), p(:,1), p(:,2));
or replicate each point so you can take advantage of the one-line-per-column input
h = plot([p(:,1) p(:,1)]', [p(:,2) p(:,2)]');
For the former two options, you'll need to either issue a hold on command for you axis prior to plotting, or use line instead of plot. The last option will work without that.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Centro de ayuda y File Exchange.

Preguntada:

el 27 de Mayo de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by