Rescale multiple spheres on the same 3d plot

I am trying to plot multipe spheres of different sizes on the same 3D plot. I'd like the spheres placed closest to the origin to be the largest and the smallest ones furtherest away. I've tried sending a vector into the function function to control how many faces the sphere will have, but I get this error. The line numebrs in the error code don't match up exaclty because I have commented out my previous attempts at fixing the problem that don't apply.
Error in sphere (line 29)
cosphi = cos(phi); cosphi(1) = 0; cosphi(n+1) = 0;
Error in BioM3D_SphereTest>createspheres (line 83)
[x, y, z] = sphere(n);
Error in BioM3D_SphereTest (line 71)
[x_loc,y_loc,z_loc, spheresXYZ{i}] = createspheres(FNA_x(i),FNA_y(i),FNA_z(i), n(i,1));
Below is my code. Any help or ideas would be appreciated.
clear
close
clc
A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969];
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
% Create Hemisphere Domain
[x_dom,y_dom,z_dom] = sphere(80); %Create Sphere
x_dom = x_dom(41:end,:); % Keep top 41 x points
y_dom = y_dom(41:end,:); % Keep top 41 y points
z_dom = z_dom(41:end,:); % Keep top 41 z points
hemisphere_radius = 80;
figure();
Hemi_sf = surf(hemisphere_radius.*x_dom,hemisphere_radius.*y_dom,hemisphere_radius.*z_dom, 'FaceColor','#4DBEEE','EdgeColor', 'none');
alpha 0.2 %Sets transparency of boundary hemisphere
axis equal
x_ax_lab = xlabel('x axis', 'Color', '#4DBEEE');
y_ax_lab = ylabel('y axis', 'Color', '#4DBEEE');
z_ax_lab = zlabel('z axis', 'Color', '#4DBEEE');
% Plot Outerboundary Circular Plane
x_c = 0;
y_c = 0;
z_c = 0;
radii_plane = 80;
radii_vein = 1;
center_plane = [x_c, y_c]; % center point of circular plane
viscircles(center_plane, radii_plane, 'color', '#77AC30');
hold on
SizeofA = size(A,1);
FNA_x = A(:, 1);
FNA_y = A(:, 2);
FNA_z = A(:, 3);
n = [6.96370042788934;5.96370042788934;5.96370042788934;4.96375547570639;5.55378214243664;4.97904924545806;3.98374650974911;5.55083110747708;5.52220023318542;4.65666303595053;3.99715084015465;2.99294670513842;4.67296636191766;4.60286314806927;3.68897172789716;3.02005588750933;1.99814346506139;3.71566225784534;3.62540440258680;2.70847851108481;2.04428077071202;1;2.74447376195869;2.63691531214705;1.72499063143428];
% Create Spheres
for i = 1:SizeofA
[x_loc,y_loc,z_loc, spheresXYZ{i}] = createspheres(FNA_x(i),FNA_y(i),FNA_z(i), n(i,1));
h(i) = surf(x_loc, y_loc, z_loc,'FaceColor', 'k');
end
function [X,Y,Z,spheresXYZ] = createspheres(spherex, spherey, spherez, n)
[x, y, z] = sphere(n);
X = (x+spherex);
Y = (y+spherey);
Z = (z+spherez);
spheresXYZ = [X,Y,Z];
end

 Accepted Answer

On the first iteration of the i-loop, n equals 6.9637.... This value is passed to sphere(n) and, as the error message indicates,
Array indices must be positive integers or logical values.
n is not an integer. In the function sphere(n), n determines the size of the outpus which can be understood as the resolution of the sphere. Matrix sizes must be positive integers.
Demo 1
To scale the size of the unit-spheres produced by sphere(n), simply multiply the x,y,z outputs by a scaling factor.
[x,y,z] = sphere(20);
clf()
hold on
surf(x,y,z,'FaceColor','b','FaceAlpha',.5)
surf(x*2,y*2,z*2,'FaceColor','r','FaceAlpha',.2)
view(3)
axis equal
grid on
Demo 2
Scale the size of n randomly placed spheres based on their distance from (0,0,0), marked by a black +.
rng(999) % for reproducibility purposes
nSpheres = 10; % Number of spheres
maxEccentricity = 100; % max distance of sphere center from (0,0)
[x,y,z] = sphere(20);
clf()
colors = jet(nSpheres);
hold on
view(3)
grid on
box on
plot3(x,y,z,'k+')
for i = 1:nSpheres
az = rand(1)*2*pi;
el = rand(1)*pi - (pi/2);
radius = randi(maxEccentricity+1)-1;
[sx,sy,sz] = sph2cart(az,el,radius);
surf(x*radius/3+sx, y*radius/3+sy, z*radius/3+sz, 'FaceColor', 'interp', 'FaceAlpha', .6, 'EdgeAlpha',.3);
end
axis equal
view([-46.943, 11.247])

9 Comments

This is incredibly detailed thank you Adam! For the first demo I just need to index into the n vector to multiply the appropriate surface data by its corresponding scaling factor right?
Yes, that's how you would scale the spheres.
But to create the sphere vertices you can't use n in sphere(n) since the values of n are not integers.
Hey Adam, I just have a few followup questions. I am trying to use the second example you provided. But I am having trouble resizing the spheres and retaining their original positions defined by the coordinates in my original A matrix above. Should I use the spherical conversions of the points, so that both the xyz coordinates and the spherical positions define the same location instead of random ones if that makes sense? Or is there an easier solution that Im not seeing? I can ask this in another question if needed.
It's easiest if you're creating the spheres centered at (0,0,0), then resize them, and then shift them to the desired (x,y,z) coordinate by merely adding the x,y,z offsets that define their centers. Does that make sense?
Yes I see what you mean.That is a lot easier than using spherical coordinates. I gonna try that out and ask it in another question if I run into problems. Thanks again!
Sounds good. Feel free to post the link to new but related questions as a comment here if you'd like to draw my attn to it.
Hey Adam so I was able generate resized spheres but I am having trouble getting them to their actual locations instead of centered at 0,0,0. Here is what I have.
Edit: I think i fixed it by adding the points again to offset them in the surface command call, but is there any way to clean the spheres up so that they are more distinct?
Edit 2: I solved my clarity problem by rescaling the vector i used to resize them by dividing by the max value in the rescaling vector. So that all the other scaling values are a fraction of the largest value. Thanks again for all your help!
clear
close
clc
% A = 25 pts
%A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969];
% A = 100 pts
A = [0,0,0;0.00977495601952172,0.0129188554738323,0.999868768093125;-0.566794094824837,-0.823750570492204,0.0133959578031223;0.0279587435128966,0.0380588867245362,1.99938731654588;0.830388266617646,0.583999369869120,0.978401571089338;-1.07826433834531,-1.64452537544960,0.267810795303313;0.168715496407312,0.263085998125572,2.96351922435162;-0.791458202545459,0.611268411797120,0.998070417818667;-1.41124221175034,-0.289407593850698,0.0506110237693017;1.69942938355116,1.04462646221878,1.15892918358242;-1.55216375501531,-2.44987966243271,0.623934110066297;0.188310075544404,0.501770043093754,3.93441879647330;-1.44603145623221,1.35107113546187,1.15371676572365;-2.35391403973316,0.0183196401577155,0.179737992978120;2.59879677816763,1.38804628951095,1.42948622326796;-1.92629974765512,-3.28302931738291,1.03122259685150;0.190461468181228,0.731318108759712,4.90771374511875;-2.01837467713375,2.14014570650778,1.37684130228734;-3.30531517848174,0.217844651708771,0.414313445558867;3.50709118523837,1.65551615551852,1.75113998255253;-2.23204460424917,-4.13488361866807,1.45650407075820;0.193343935566412,0.934427321909631,5.88686559182864;-2.51244231909718,2.97180232130260,1.63030617210704;-4.25650570961388,0.357419056329789,0.689550723301627;4.41845060685608,1.87363023778730,2.10021053665969;-2.47842156351806,-4.99324597672323,1.90651791078471;0.204283419234320,1.13357858075284,6.86677329350173;0.617793464714887,0.0559247730198438,6.10612769252045;-2.96417360323821,3.81704174520463,1.91580426879081;-5.20242439373799,0.476408999682868,0.991344090368801;5.32672360772571,2.06445068867013,2.47253796167180;-2.69346338350278,-5.85030685131976,2.37470982965291;0.218591936197924,1.32878562119851,7.84743096960627;-3.26247612265381,-4.91985596911837,1.29018005288354;1.20197539596291,-0.580347098756520,6.61000225666269;-3.37592827171665,4.67443410026770,2.22457026832058;-6.14584567995603,0.584770610127789,1.30473528056482;6.23299868743130,2.22581272192086,2.86321400922248;-2.89467385558292,-6.70726873056154,2.84918921113506;0.209717491582492,1.53184472980862,8.82655723451965;5.28440710514136,1.17097130314088,2.91964410258613;0.576278852478300,2.24513662818586,7.66753960818188;-4.04653068178957,-4.84646596151352,0.673842194982372;1.72465217643194,-1.18852390077965,7.20743841278270;-3.76500316313530,5.53715317053961,2.54758193166533;-7.08499357301506,0.666849489054822,1.63829830698843;7.13773556686645,2.38268963160310,3.25924533904186;-3.08850223839479,-7.56458389242937,3.32609724340970;0.176682672356017,1.77636612069925,9.79563823738556;5.70892694024996,0.283366758099107,3.09836478191717;0.834848920291403,3.20098548783449,7.80716223363894;2.23645813228566,-1.80307906039568,7.80775218019269;-4.13663420978661,6.40608142770811,2.87446730769027;-8.01389420090031,0.739303774953617,2.00147044244114;8.04005085003118,2.54735749010561,3.65763161659614;-3.28567882346993,-8.41947317913047,3.80597816799600;0.151573909742903,2.03767830086445,10.7605659520551;-6.82918401328041,0.278600280775844,2.52363667248442;-3.41363523840503,-7.08870431609608,4.14330417016278;6.35396040499232,-0.474663609164808,3.19491420795479;1.06216210967955,4.10256481833496,8.17524296492560;2.75275937858220,-2.40442829800382,8.41751616527277;-4.49887555491066,7.27474744137621,3.21240304800297;-8.93747200633802,0.809847032829654,2.37833604988111;8.93679366464217,2.75035659507643,4.05087923302196;-3.49679202503365,-9.27121011623096,4.28553567873291;0.120474891403918,2.30220475044321,11.7244428158357;-6.57337445354577,-0.109648927503134,3.40897503798041;-3.90131441220521,-6.94494653825290,5.00440969491056;7.05097560070378,-1.18529470189817,3.29069138158143;1.32262284552297,4.97356081042056,8.59180771001900;3.28964279764057,-2.99261263184027,9.02232637002263;-4.85227421135047,8.14355948795292,3.55920993569369;-9.85594939518955,0.878289437178573,2.76784204913582;9.82550611659926,2.98592839011066,4.44419349302377;-3.72248319429006,-10.1171476702764,4.76870557719025;0.0788524583019931,2.54046289454804,12.6947523694476;2.70499218347696,-3.21391766868317,7.83232794441650;-2.84203085926377,-9.70887276953909,3.66930583711619;-6.31756489381112,-0.497898135782113,4.29431340347640;-4.51675110863053,-6.94038338635526,5.79258280428302;7.75826716048403,-1.87839080959415,3.42981878707837;1.60739083428736,5.83327554833172,9.01583337197052;3.81380630124427,-3.57090289370221,9.64749274349896;-5.21110077179350,9.00941019096056,3.90784450027642;-10.7701023523524,0.936847376158581,3.16895966118096;10.7111773842992,3.23085322898537,4.83865290649108;-3.95158917741763,-10.9629681728992,5.25047121091495;0.0248941422755761,2.76290060345524,13.6682049701363;-3.96468559986903,8.59207273742804,3.45422268973630;-4.53038653298116,-10.2195319906366,4.18835254129213;-0.734431529015096,3.08504028776024,12.4898020857790;-2.18726969349390,-10.1465354228472,3.05307599549947;-5.19378789303215,-7.06258274529947,6.51831588830272;8.48339326062922,-2.55215288868641,3.57207489297718;1.87742850700644,6.69346716517060,9.44844178339196;4.34166392226097,-4.13247115672692,10.2846798572246;-5.56586475306079,9.87609452074379,4.25855918503254;-11.6790955983398,1.00013474235130,3.58093780141378;11.5912722790929,3.48104407126623,5.24218421704439];
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
x_c = 0;
y_c = 0;
z_c = 0;
Center_Root = [x_c, y_c, z_c];
[x_dom,y_dom,z_dom] = sphere(80); % Create Sphere
x_dom = x_dom(41:end,:); % Keep top 41 x points
y_dom = y_dom(41:end,:); % Keep top 41 y points
z_dom = z_dom(41:end,:); % Keep top 41 z points
hemisphere_radius = 80;
figure();
Hemi_sf = surf(hemisphere_radius.*x_dom,hemisphere_radius.*y_dom,hemisphere_radius.*z_dom, 'FaceColor','#4DBEEE','EdgeColor', 'none');
alpha 0.2
hold on
radii_plane = 80;
center_plane = [x_c, y_c]; % center point of circular plane
viscircles(center_plane, radii_plane, 'color', '#77AC30');
dist1a = pdist2(A,Center_Root);
s = (max(dist1a) - dist1a + 1);
s = s./max(dist1a);
SizeofA = size(A,1);
% Create Spheres
axis equal
x_ax_lab = xlabel('x axis', 'Color', '#4DBEEE');
y_ax_lab = ylabel('y axis', 'Color', '#4DBEEE');
z_ax_lab = zlabel('z axis', 'Color', '#4DBEEE');
for i = 1:SizeofA
[x_loc,y_loc,z_loc, spheresXYZ{i}] = createspheres(x(i),y(i),z(i), s(i));
h(i) = surf(x_loc+x(i), y_loc+y(i), z_loc+z(i), 'FaceColor', 'interp', 'FaceAlpha', .5);
end
%view([-46.943, 11.247]);
%%
for i = 1:SizeofA
Bx{i}= h(i).XData;
By{i} = h(i).YData;
Bz{i} = h(i).ZData;
end
Bx = cell2mat(Bx);
By = cell2mat(By);
Bz = cell2mat(Bz);
%%
% Call Surf2stl
%surf2stl('extracttest.stl',Bx,By,Bz);
%%
function [X,Y,Z,spheresXYZ] = createspheres(spherex, spherey, spherez, s)
[x, y, z] = sphere(11);
X = (s*x)+spherex;
Y = (s*y)+spherey;
Z = (s*z)+spherez;
spheresXYZ = [X,Y,Z];
end
" is there any way to clean the spheres up so that they are more distinct?"
You could use color
% Define colormap
cmap = jet(SizeofA);
for i = 1:SizeofA
[x_loc,y_loc,z_loc, spheresXYZ{i}] = createspheres(x(i),y(i),z(i), s(i));
h(i) = surf(x_loc+x(i), y_loc+y(i), z_loc+z(i), 'FaceColor', cmap(i,:), ...
'FaceAlpha', .5, 'EdgeColor', 'k', 'EdgeAlpha', .5);
end
And you could zoom into the spheres
xlim([min(Bx(:)),max(Bx(:))])
ylim([min(By(:)),max(By(:))])
zlim([min(Bz(:)),max(Bz(:))])
view([-43,38])
Thank you the color map enhances the image immensely!

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!