How to take a horizontal slice of a 3D Surface plot?

What I am presented is with 3 sets of data that are supposed to fit on the x,y,z axis and create a 3D plot with no predefined function relating all of them. I first created a surface fit with cubic interpolation to generate a piecewise cubic interpolant and then I plotted this on a 3D graph. Shown below is the code I used for that:
dataset = xlsread('3DSurfaceData.xlsx', 'Sheet1','A1:C101')
x3 = dataset(:,1);
y3 = dataset(:,2);
z3 = dataset(:,3);
sf = fit([x3,y3],z3,'cubicinterp');
plot(sf,[x3,y3],z3)
xlabel('x')
ylabel('y')
zlabel('z')
hold on
patch([51.1,51.1,51.2,51.2],[-154.05,-154.13,-154.13,-154.05],[0.25,0.25,0.25,0.25],'w','FaceAlpha',0.7);
plot(sf, 'Style', 'Contour');
However, what I want to do now is take a horizantal slice of the surface plot at z=0.25 (as visualized by the patch) and put that data on a 2D plot. Or alternatively create a 2D contour plot at that z-value.
I've succesfully created vertical slices by using the interp2 function to interpolate z-values along the slice by using the x and y values along that slice. But I need to know how to do that horizantally.

7 Comments

If you had an array of x, y, z values, then
contour(x, y, z, [0.25 0.25])
would result in a contour at only 0.25 level.
Must z be a matrix of certain size for this to work?
Also, it says that Vector X must be strictly increasing or strictly decreasing with no repeated values.
You have vectors of values, not arrays of values. You would use something like griddedInterpolant to create arrays of z values.
So I use F=scatteredInterpolant(x3,y3,z3, 'nearest') to build a relationship between the values. How would I then go about creating an array of Z-values to be used in the contour function you described above?
I was thinking about using meshgrid but am unsure of what range of values to carry out the meshgrid over.
But say I did:
X=meshgrid(51.1:0.01:51.2)
Y=meshgrid(-154.05:-154.13,100)
Then would my contour function look like this?
zq=F(X,Y)
contour(x3, y3, zq, [0.25 0.25])
That clears things up somewhat. However I am rather confused on what the M values are in your code:
F = griddedInterpolant(x, y, z, M);
Xv = linspace(min(x), max(x), 20);
Yv = linspace(min(y), max(y), 20);
Zv = linspace(min(z), max(z), 20);
[Xg,Yg, Zg] = ndgrid(Xv, Yv, Zv);
Mg = F(Xg, Yg, Zg)
Where does it come from and what values does it signify?
"I have a dataset containing the x,y,z points as seperate entries (each a size of 100043) and M (also 100043) as a seperate entry in the workspace. "
That particular user has a scattered volume dataset, with coordinates x, y, z, and associated value stored in M.
Your situation would probably use 2D, like
N = 50;
F = scatteredInterpolant(x, y, z);
Xv = linspace(min(x), max(x), N);
Yv = linspace(min(y), max(y), N);
[Xg,Yg] = ndgrid(Xv, Yv);
Zg = F(Xg, Yg);
contour(Xg, Yg, Zg, [0.25 0.25])

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!