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.

 Accepted Answer

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

All I had to do was transpose my Z matrix as well, this worked perfectly. Thank you very much.
As always, my pleasure!
This was an interesting problem!
Hi Star Strider,
This is really an interesting problem to see and thank you for solution.
However, what would I need to change if I wanted to get values of Z aboe the line which goes from [-4,-4] to [4,4] ( this is a diagonal under the surface but don't have to be always). I am strugling with this for couple of weeks now and by finding the above answer conviced me that I will rather ask someone for help.
Thank you in advance!
Petr
@Petr Miarka I do not completely understand, however I did my best to interpret your request as best as I could. This is an interesting — and very relevant — problem so I am addreessing it here. (The online Run feature was not available when I originally posted my code, so I am also running it here to refer to it.)
I included the original code, and an expansion of it that takes an arbitray linear ‘slice’ over the surface. I made it as robust as I can, since this addresses all the complications I can think of just now.
[X,Y,Z] = peaks(75); % 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))
xv = X(1,:); % X-Vector
Yarb = @(x) 2*x + 1; % Arbitrary Slice: y(x) = 2*x+1
yv = Yarb(xv); % Calculate Corresonding Y-Vector
Lv = yv>=min(Y(:,1)) & yv<=max(Y(:,1)); % Prevent Plotting Beyond The Surface Limits
xv = xv(Lv);
yv = yv(Lv);
F = scatteredInterpolant(X(:), Y(:), Z(:)); % Create Interpolation Function
surfline = F(xv,yv); % Calculate 'Slice' Z-Vector From (x,y) Coordinates
figure
surf(X,Y,Z, 'FaceAlpha',0.5, 'EdgeAlpha',0.5)
hold on
plot3(xv, yv, surfline, '-r', 'LineWidth',3)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface Plot With Arbitrary Line')
legend('Peaks Function Surface', 'Arbitrary Line Across Surface', 'Location','best')
view(20,30)
figure
surf(X,Y,Z, 'FaceAlpha',0.5, 'EdgeAlpha',0.5)
hold on
plot3(xv, yv, surfline, '-r', 'LineWidth',3)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Surface Plot With Arbitrary Line (Plan View)')
legend('Peaks Function Surface', 'Arbitrary Line Across Surface', 'Location','best')
view(0,90)
% axis('equal')
figure
plot(xv, surfline, '-r', 'LineWidth',3)
grid
xlabel('Y')
ylabel('Z')
title(sprintf('Slice Through Surface At ‘surfline’', Xval))
xlim([-3 3])
The ‘Yarb’ function can of course be anything reasonable (and even nonlinear, although that might be more difficult to plot in 2D) so long as it does not stray outside the limits of the grid used to plot the surface.
I have done something like this previously in another Answer, however not this particular application of it. This is an interesting problem, and definitely adds to my original Answer.
.
@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
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!