4
$\begingroup$

I am trying to create a series of randomly distributed spheres in Matlab, three will be three sub-sets. I would like each subset to have a different color. I am using a structure because each sphere will also have different properties associated with it. I tried using colormap, and set(...'FaceColor'..) but am having some difficulty. Please Help.

for n = 1:100
    Bslice.cell(n).index = n; 
    Bslice.cell(n).type = 'Tyep1'; % Type2, Type3
     Bslice.cell(n).location = round(rand(1, 3)*10);
end 

%%
[x,y,z] = sphere;

for n = 1:10
    hold on 
    grid on
    surfl(x-Bslice.cell(n).location(1), y-Bslice.cell(n).location(2), z-        Bslice.cell(n).location(3), ones(size(); 
    shading interp
    % colormap(hot(100))
end
$\endgroup$
2
  • $\begingroup$ Hi Cerberus, and welcome to scicomp! Excellent question! I've been wondering about how to do this myself too. $\endgroup$
    – Paul
    Nov 30 '12 at 22:17
  • $\begingroup$ Hi, Cerberus, I wonder why have you accepted my answer, you haven't said which one is you need so that I could add more details about tweaking the colors. $\endgroup$
    – Shuhao Cao
    Dec 3 '12 at 20:45
1
$\begingroup$

Let me be clear, FaceColor assigns a specific single color to every surface in that patch object drawn by surfl, and you would lose the lighting info.

If you want to modify FaceColor for each ball, you could add a color to indicate the color(here I use the distance to the origin), rescale it to an RGB vector(also I assume Bslice.cell(n).location is the direction of the lighting source in surfl)

clear all;close all;
for n = 1:100
    Bslice.cell(n).index = n; 
    Bslice.cell(n).type = 'Tyep1'; % Type2, Type3
    Bslice.cell(n).location = round(rand(1, 3)*10);
    color(n) = sqrt(sum(Bslice.cell(n).location.^2));
end 
color = repmat(color,3,1)/max(color);

[x,y,z] = sphere;

for n = 1:10
    hold on 
    grid on
    h(n) = surfl(x-Bslice.cell(n).location(1), ...
          y-Bslice.cell(n).location(2), ...
          z-Bslice.cell(n).location(3), ...
          Bslice.cell(n).location); 
set(h(n),'FaceColor',color(:,n))
view(3);
end

sphere1

Or you mean for each set you are using a different colormap like this?

sphere2

sphere3

sphere4

$\endgroup$
1
  • $\begingroup$ This is very helpful. I will have a range of data [1 to 100] for each Sphere and eventually want the spheres to change color based on their range value. 1 = balck, 100= bright yellow. also Bslice.cell(n).location is not a lighting parameter but the location of the sphere in three denominational space. $\endgroup$
    – Cerberus
    Dec 4 '12 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.