Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

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
share|improve this question
    
Hi Cerberus, and welcome to scicomp! Excellent question! I've been wondering about how to do this myself too. –  Paul Nov 30 '12 at 22:17
    
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. –  Shuhao Cao Dec 3 '12 at 20:45
    
Thanks Paul! Do you have any suggestions! –  Cerberus Dec 4 '12 at 0:29
    
Thanks! Shuhao--just responded to your post. –  Cerberus Dec 4 '12 at 1:04
add comment

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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. –  Cerberus Dec 4 '12 at 0:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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