How do I assign colours to every plotted dot if I have the colours as cell arrays

1 view (last 30 days)
I have an x-position matrix and a y-position matrix eg. x=[1,2,3,4] y=[1,2,3,4] nad I also have a cell array for the colours of each position eg, c = [r g b r]. How can I plot the data so that each dot on the plot corresponds to the colour, eg, positon 1,1 is red position 2,2 is green

Answers (1)

DGM
DGM on 29 Apr 2022
Normally, you'd do this by using a colortable (or a list of indices into a color table) with scatter().
x = 1:4;
y = 1:4;
c = [1 0 0; 0 1 0; 0 0 1; 1 0 0];
scatter(x,y,40,c,'filled');
If you really want to do it using a cellchar of color names, I'm pretty sure you're going to be stuck using a loop.
x = 1:4;
y = 1:4;
c = {'r','g','b','r'};
for k = 1:numel(x)
plot(x(k),y(k),'.','color',c{k},'markersize',20);
hold on
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!