Try this:
[X,Y,Z] = peaks(50);
figure
surf(X,Y,Z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface Plot')
Xval = 0.5;
Yp = interp1(X(1,:), Y.', Xval);
Zp = interp1(X(1,:), Z, Xval);
figure
plot(Yp, Zp, '-r')
grid
xlabel('Y')
ylabel('Z')
title(sprintf('Slice Through Surface At X = %.3f', Xval))
Since the ‘X’ value you want may not correspond to an existing ‘X’ value in the computed surface, this interpolates to produce the appropriate vectors at any ‘X’ value within the defined range of the surface.
Note that here the ‘Y’ matrix is transposed, since it was apparently created by meshgrid. It may be necessary to experiment by transposing the matrices depending on the particular surface created, and how they were defined (meshgrid or ndgrid, since their outputs are transposes of each other). My code here appears to work with the peaks function I chose to test it.
Best Answer