Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
2D slice from 3D surf

Subject: 2D slice from 3D surf

From: Alexis S

Date: 17 Jun, 2007 09:45:44

Message: 1 of 3

Hi people,

I can't seem to find a way to get a slice from
a 3D surface.

I have my 3D points as BndX, BndY, BndZ which are 256x1 matrices.

then

xlin = linspace(min(BndX),max(BndX),43);
ylin = linspace(min(BndY),max(BndY),43);
[X,Y] = meshgrid(xlin,ylin);
Z = griddata(BndX,BndY,BndZ,X,Y,'cubic');

so I can plot a solid surface using surf :

surf(X,Y,Z);

My goal is to get one slice from this interpolated surface (of course
this will be just a boundary line around the hollow surface) for a
specific value of one the axes.

I was looking at the slice command but this seems to require an extra
variable V (for volume) which in the example is a function of X,Y,Z
data.

Any help appreciated!

Subject: 2D slice from 3D surf

From: marky

Date: 18 Jun, 2007 10:07:49

Message: 2 of 3

Alexis S wrote:
> Hi people,
>
> I can't seem to find a way to get a slice from
> a 3D surface.
>
> I have my 3D points as BndX, BndY, BndZ which are 256x1 matrices.
>
> then
>
> xlin = linspace(min(BndX),max(BndX),43);
> ylin = linspace(min(BndY),max(BndY),43);
> [X,Y] = meshgrid(xlin,ylin);
> Z = griddata(BndX,BndY,BndZ,X,Y,'cubic');
>
> so I can plot a solid surface using surf :
>
> surf(X,Y,Z);
>
> My goal is to get one slice from this interpolated surface (of course
> this will be just a boundary line around the hollow surface) for a
> specific value of one the axes.
>
> I was looking at the slice command but this seems to require an extra
> variable V (for volume) which in the example is a function of X,Y,Z
> data.
>
> Any help appreciated!

How about this:

[X,Y,Z] = peaks(30);
Zlevel = 0.5;
[C,h] = contour(X,Y,Z,[Zlevel]);

You should get the X and Y slices in a similar manner.

Subject: 2D slice from 3D surf

From: Eric Carlson

Date: 18 Jun, 2007 09:20:29

Message: 3 of 3


>>
> I have my 3D points as BndX, BndY, BndZ which are 256x1 matrices.
>
> then
>
> xlin = linspace(min(BndX),max(BndX),43);
> ylin = linspace(min(BndY),max(BndY),43);
> [X,Y] = meshgrid(xlin,ylin);
> Z = griddata(BndX,BndY,BndZ,X,Y,'cubic');
>

This is brute force, but I think this may be what you want. I have not
tested any of it, but it should give you an idea of how to get any slice
from your data.

%BndX, BndY, BndZ already defined
Zsurf = @(X,Y) griddata(BndX,BndY,BndZ,X,Y,'cubic');%define surface func

%any slice can be extracted by defining x(t), y(t)
% and evaluating Zsurf(x(t),y(t))
%constant x
x_c=BndX(1)%constant value of x
x1=@(t) x_c*ones(size(t));
y1=@(t) t;
z1=@(t) Zsurf(x1(t),y1(t));
Yplot=linspace(min(BndY),max(BndY),21);
plot(Yplot,z1(Yplot))

%constant y
y_c=BndY(1)%constant value of y
y2=@(t) y_c*ones(size(t));
x2=@(t) t;
z2=@(t) Zsurf(x2(t),y2(t));
Xplot=linspace(min(BndX),max(BndX),21);
plot(Xplot,z2(Xplot))

%along line from (min x,min y) to (max x, max y)
slope=(Yplot(2)-Yplot(1))/(Xplot(2)-Xplot(1));
y_line==@(x) slope*(x-Xplot(1))+Yplot(1);
x3=@(t) t;
y3=@(t) y_line(x3(t));
z3=@(t) Zsurf(x3(t),y3(t));
plot(Xplot, z3(Xplot))

%along an ellipse
Xc=(min(BndX)+max(BndX))/2;
Yc=(min(BndY)+max(BndY))/2;
rX=(max(BndX)-(min(BndX))/2;
rY=(max(BndY)-(min(BndY))/2;
x4=@(t) Xc+rX*cos(2*pi*t);
y4=@(t) Yc+rY*sin(2*pi*t);
z4=@(t) Zsurf(x4(t),y4(t));
Tplot=linspace(0,1,41);
plot(Tplot, z4(Tplot))

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us