how to get 2d line of surface plot section view

11 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 9 Nov 2020
Try this:
[X,Y,Z] = peaks(50); % Create Surface Data
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.
  6 Comments
Petr Miarka
Petr Miarka on 24 Jan 2022
@Star Strider Thank you, that's what I meant!
Right after I've posted my previous question, I figure it out myself based on your original comment. I have used interp2 function instead of scatteredInterpolant, it works too! However I think your code is more elegant and I will use it my application!
Thank you a lot for your time!
Petr Miarka
Star Strider
Star Strider on 24 Jan 2022
My pleasure!
(A Vote would be apprecaited!)
.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!