MATLAB: Make a 3D surface plot instead of plot3

3dcontourdatapointsdistortextrapolateinterpolateMATLABplotpointsrectangularscatterscatterplotsquaresurfsurface

I have 3D data that I can plot using "plot3" as follows:
>> plot3(x, y, z)
View from above shows the circular shape of the data:
I want to be able to plot it as a surface.
I am concerned about preserving the circular shape of the data without any distortion. I am also not sure if extrapolation at locations where the data is not present is necessary.

Best Answer

The following code should accomplish plotting the data as a 3D surface:
[xi, yi] = meshgrid(x,y);
F = scatteredInterpolant(x,y,z);
zi = F(xi,yi);
surf(xi,yi,zi, 'EdgeAlpha', 0)
hold on
plot3(x, y, z)
The "surf" function needs a grid in the X-Y plane and values in Z for each point on the grid in order to plot a surface. This will result in a square surface patch. Original data is not sufficient to plot a surface as it is just scattered X, Y and Z data. This is why we need to use interpolation (e.g. "scatteredInterpolant" function), and both interpolate the points in-between the original data, and extrapolate for points the corners of the grid (outside the outer "contour"). You can control how fine of a mesh you want to use. In the above example we have
[xi, yi] = meshgrid(x,y);
which created a grid line for every single point in the original data, so a very fine mesh. For comparison, consider using only 100 grid lines evenly spaced between maximum and minimum values of x and y:
xgrid = linspace(min(x), max(x), 100);
ygrid = linspace(min(y), max(y), 100);
[xi, yi] = meshgrid(xgrid, ygrid);
You can also change the interpolation method (by default it is 'linear'). Even with the "square" surface, you should be able to see that the interpolated surface coincides well with the original data (by plotting "plot3" over the surface):
View from above:
To emphasize, the resultant shape is a square because we needed to use a grid to create the surface. For cosmetic reasons, you could exclude points outside the outer contour from the mesh. But you would still need to use interpolation to create the surface.