MATLAB: How to change the color of a single data point of a scatter plot by using handles

colorhandles

h = scatter([x1 x2 x3; y1,y2,y3])
I have tried using the following code but it changes the the color of all the data points. I thought it would set the first point stored in the array to red instead of all the points.
set(h([1]),'CData',[1 0 0])

Best Answer

No, the CData holds the colors of all of the points. So if there's just one color, that's going to be the color of all of the points.
What you would need to do is take the color, replicate out to the number of points, and then replace one copy. Consider this example:
npts = 5;
x = randn(1,npts);
y = randn(1,npts);
h = scatter(x,y,'filled');
c = h.CData;
% c is now a 1x3, meaning a RGB color that's used for all of the points
c = repmat(c,[npts 1]);
% c is now a 5x3 containing 5 copies of the original RGB
c(1,:) = [1 0 0];
% c now contains red, followed by 4 copies of the original color
h.CData = c;
% Now the scatter object is using those colors