Nemanja Boric
Nemanja Boric

Reputation: 22157

Get colormap color for a given value

I have function which plots some 3D figure with

scatter3(mx,my,mz,3,mx.^2+my.^2); % mx, my and mz are vectors

I see that C is a vector the same length as X and Y, so color of each point should be linearly mapped to the colors in the current colormap, per documentation.

I tried this:

cmap = colormap;
disp(cmap(mx.^2+my.^2));

but I am getting

Subscript indices must either be real positive integers or logicals.

Is there any easier way to solve this quest?

Thanks

Upvotes: 1

Views: 6648

Answers (1)

s-m-e
s-m-e

Reputation: 3721

That's rather easy. Colormap does not return a vector, but a matrix. This is because each colour has three components (red, green and blue).

>> size(colormap)

ans =

    64     3

>> test = colormap;
>> test(7, :)

ans =

     0         0    0.9375

EDIT ... And, I forgot something: The indices need to be integers, in one way or another. You may like to round them or turn them into an integer.

EDIT2 ... According to your example, the disp-statement works like this:

disp( cmap(1:( (size(cmap, 1)-1) / (length(mz)-1) ):size(cmap, 1), :) );

Upvotes: 2

Related Questions