MATLAB: How to get 2d line of surface plot section view


I have a surface I have made, and want to see what the Y-Z 2D curve would look like on a specific plane perpendicular to the X axis. Effectively, I want to take a section view of the YZ plane at a specific value along the X axis, and get the data points from that curve. Slice does not appear to be appropriate. I have tried to adjust the code found here to no avail.

Best Answer

  • Try this:
    [X,Y,Z] = peaks(50); % Create Surface Data
    grid on
    title('Surface Plot')
    Xval = 0.5;
    Yp = interp1(X(1,:), Y.', Xval);
    Zp = interp1(X(1,:), Z, Xval);
    plot(Yp, Zp, '-r')
    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.