You're creating a lot of cylinders which each have a constant radius? If so, then something like this should work:
cla
hold on
for i=1:100
r = 1+rand(1);
[x,y,z] = cylinder(r,10);
surf(x,y,z+i,r*ones(size(z)),'EdgeColor','none');
end
view(3)
colorbar
What's going on there is that the 4th argument to surf is a CData array which is used for the face colors.
Of course, you could also combine them into a single surf, like this:
cla
hold off
x = [];
y = [];
z = [];
c = [];
for i=1:100
r = 1+rand(1);
[xt,yt,zt] = cylinder(r,10);
x(end+1,:) = xt(1,:);
y(end+1,:) = yt(1,:);
z(end+1,:) = i*ones(size(zt(1,:)));
c(end+1,:) = r*ones(size(zt(1,:)));
end
surf(x,y,z,c,'EdgeColor','none');
Best Answer