Rescale multiple spheres on the same 3d plot
9 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Adam Danz
2020 年 10 月 5 日
編集済み: Adam Danz
2020 年 10 月 5 日
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 件のコメント
Adam Danz
2020 年 10 月 15 日
" 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])
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!