Aiuto con il comando surf di una matrice di dati

2 views (last 30 days)
Vincenzo
Vincenzo on 30 Jul 2025
Edited: saish on 5 Aug 2025
Buongiorno,
ho una matrice di dati [x,y,z] e vorrei eseguire una rappresentazione 3d con barra di colore per la profondità lungo l'asse z. Ho provato con il comando
surf([x x], [y y], [z z], 'opzioni per il colore della faccia, del bordo ecc...')
dal momento che l'argomento z deve essere necessariamente una matrice, tuttavia oltre alla superficie che mi aspetto ce n'è un'altra indesiderata.

Answers (1)

saish
saish on 5 Aug 2025
Edited: saish on 5 Aug 2025
Hi Vincenzo,
I observed that [x x] duplicates the column vector horizontally, but does not create a grid. The MATLAB ‘surf’ function expects X, Y, and Z to be matrices of the same size, where each (X(i,j), Y(i,j), Z(i,j)) is a point on the surface.
If the data is not on a grid, duplicating vectors does not create a real surface, it creates a flat “sheet” or an unwanted extra surface.
The MATLAB ‘meshgrid’ function can be used to create a grid of points covering a rectangular area in the x-y plane.
The function takes two vectors (x and y) as input and creates two 2D matrices: one containing all x-coordinates and the other containing all y-coordinates.
You can refer to the following code snippet, which uses the ‘surf’ function together with the ‘meshgrid’ function:
x = [1 2 3];
y = [4 5 6];
[X, Y] = meshgrid(x, y);
Z = [7 8 9; 10 11 12; 13 14 15]; % to match grid size
% Correct 3D surface plot
figure;
surf(X, Y, Z, 'FaceColor', 'interp', 'EdgeColor', 'none');
colorbar;
xlabel('X');
ylabel('Y');
zlabel('Z');
Additionally, for scattered data, you can use the MATLAB ‘griddata’ function which can interpolate the data onto a grid, which can be used with the ‘surf’ function.
You can refer to the following Matlab documentation and examples for reference.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!