How can I display a slice through a surface?

28 views (last 30 days)
I have a surface which is an ellipsoid, and I would like to display the cross-section of the ellipsoid at specific planes.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jul 2009
The SLICE command requires a volume of data defined on a uniformly spaced grid. Since a surface is defined by a set of vertices (usually on a nonuniformly spaced grid), use the GRIDDATA3 function to fit the surface to a uniformly spaced grid. You can then use the SLICE function to plot a slice through this volume, as illustrated by the following example:
[x, y, z] = ellipsoid(0,0,0,10,10,10,20);
ti = -10:.25:10;
[XI,YI, ZI] = meshgrid(ti,ti, ti);
v = ones(size(x));
V = griddata3(x,y,z,v,XI,YI, ZI);
figure;
h = surf(x, y, z); hold on
set(h, 'FaceAlpha', 0.2,'EdgeAlpha', 0.2)
h1 = slice(XI, YI, ZI, V, [], [9 0 -9], []);
for i = 1:numel(h1)
hi = h1(i);
cdata = get(hi, 'CData');
cdata(~isnan(cdata))=1;
set(hi, 'CData', cdata, 'EdgeColor', 'none')
end
The method described above works for a general surface, but the time required to fit the surface to a fine grid is large. Alternatively, if the surface has a mathematical definition, as in the case of the ellipsoid, you can calculate the volume directly, as in the following example:
xc = 0;
yc = 0;
zc = 0;
xr = 10;
yr = 10;
zr = 10;
[x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr,20);
ti = -10:.25:10;
[XI,YI, ZI] = meshgrid(ti,ti, ti);
V = (XI - xc).^2/xr^2+(YI - yc).^2/yr^2+(ZI - zc).^2/zr^2;
V(V>1)=NaN;
figure;
h = surf(x, y, z); hold on
set(h, 'FaceAlpha', 0.2,'EdgeAlpha', 0.2)
h1 = slice(XI, YI, ZI, V, [], [9 0 -9], []);
for i = 1:numel(h1)
hi = h1(i);
cdata = get(hi, 'CData');
cdata(~isnan(cdata))=1;
set(hi, 'CData', cdata, 'EdgeColor', 'none')
end
This code displays three slices through an ellipsoid.
  1 Comment
Jonathan Kwang
Jonathan Kwang on 25 Apr 2016
Edited: Jonathan Kwang on 25 Apr 2016
You can call figure() before plot to have each plot in 3 separate figures. Example:
figure()
plot(x,y)
figure()
plot(y,z)
figure()
plot(x,z)
Or you can have 3 separate plots in 1 figure. Example:
subplot(2,2,1);
plot(x,y);
title('x vs y');
subplot(2,2,2);
plot(y,z);
title('y vs z');
subplot(2,2,3);
plot(x,z);
title('x vs z');

Sign in to comment.

More Answers (0)

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!