MATLAB: Parts of the graphs are missing

graphpatchprogramming

i want to graph an ellipsoid with 3 sections for an ellipsoid for example i want half of the ellipsoid disapear from 3 different sides , but when i write the code, the graph that appears to me is quarter of the graph
the code:
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
figure
subplot(2,2,1)
surf(x, y, z)
colormap cool
hold on
patch([0,0,0,0],[-4,-4,4,4],[-4,4,4,-4],'w','FaceAlpha',0.7);
patch([-6.5,-6.5,6.5,6.5],[0,0,0,0],[-4,4,4,-4],'w','FaceAlpha',0.7);
patch([-6.5,6.5,6.5,-6.5],[-4,-4,4,4],[0,0,0,0],'w','FaceAlpha',0.7)
axis equal
hold off
filter1 = x<0
subplot(2,2,2)
x(filter1) = nan
y(filter1) = nan
z(filter1) = nan
surf(x,y,z)
axis equal
filter2 = y<0
subplot(2,2,3)
x(filter2) = nan
y(filter2) = nan
z(filter2) = nan
surf(x,y,z)
axis equal
filter3 = z>0
subplot(2,2,4)
x(filter3) = nan
y(filter3) = nan
z(filter3) = nan
surf(x,y,z)
axis equal

Best Answer

You remove the points from the same x, y and z values step by step. Store the original values in x0, y0, z0 instead:
[x0, y0, z0] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
figure
subplot(2,2,1)
surf(x0, y0, z0)
colormap cool
hold on
patch([0,0,0,0],[-4,-4,4,4],[-4,4,4,-4],'w','FaceAlpha',0.7);
patch([-6.5,-6.5,6.5,6.5],[0,0,0,0],[-4,4,4,-4],'w','FaceAlpha',0.7);
patch([-6.5,6.5,6.5,-6.5],[-4,-4,4,4],[0,0,0,0],'w','FaceAlpha',0.7)
axis equal
hold off
filter1 = x0 < 0;
subplot(2,2,2)
x = x0;
y = y0;
z = z0;
x(filter1) = nan;
y(filter1) = nan;
z(filter1) = nan;
surf(x,y,z);
axis equal
filter2 = y0 < 0;
subplot(2,2,3)
x = x0;
y = y0;
z = z0;
x(filter2) = nan;
y(filter2) = nan;
z(filter2) = nan;
surf(x,y,z)
axis equal
filter3 = z0 > 0;
subplot(2,2,4)
x = x0;
y = y0;
z = z0;
x(filter3) = nan;
y(filter3) = nan;
z(filter3) = nan;
surf(x,y,z)
axis equal
By the way, avoid repeated code by using functions:
function main
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
figure;
subplot(2, 2, 1)
surf(x, y, z)
colormap cool
hold on
patch([0,0,0,0], [-4,-4,4,4], [-4,4,4,-4], 'w', 'FaceAlpha', 0.7);
patch([-6.5,-6.5,6.5,6.5], [0,0,0,0], [-4,4,4,-4], 'w', 'FaceAlpha', 0.7);
patch([-6.5,6.5,6.5,-6.5], [-4,-4,4,4], [0,0,0,0], 'w', 'FaceAlpha', 0.7)
axis equal
hold off
subplot(2,2,2);
DrawFilteredData(x, y, z, x0 < 0);
subplot(2,2,3);
DrawFilteredData(x, y, z, y0 < 0);
subplot(2,2,4);
DrawFilteredData(x, y, z, z0 > 0);
end
function DrawFilteredData(x, y, z, F)
x(F) = nan;
y(F) = nan;
z(F) = nan;
surf(x,y,z);
axis equal
end