MATLAB: Plot cross section at particular value of x for mesh(x,y,z).

3d plotsplot

I have a mesh plot mesh(x,y,z) where x,y and z are having 100 by 100 values. Now I want a cross section plot at a particular value of x. How to do this?

Best Answer

You can easily adapt this to your data. I chose different lengths of the ‘x’ and ‘y’ vectors to demonstrate and trace the indexing of the matrices:
x = linspace(5, 10, 80); % Create Data



y = linspace(1, 5, 50); % Create Data
[X,Y] = meshgrid(x,y); % Create Data
Z = sin(X.^2) - cos(Y.^2); % Create Data
figure(1)
surf(X, Y, Z) % Surface Plot
grid on
X_idx = find(x >= 7.5, 1, 'first'); % Find Desired ‘X’ Value
figure(2)
plot(Y(:,X_idx), Z(:,X_idx)) % Plot At Desired ‘X’ Value
grid