# Creating 2D contours from XYZ values

79 views (last 30 days)
Wesley Brown on 18 May 2020
Commented: Star Strider on 31 May 2020
I need to create a 2D contour map from the following X,Y,Z values:
x =
62158.9992780663
-60909.305073411
-66390.9511725443
39368.6900415121
51352.5822375015
-21369.5783460914
-87076.7531674554
49625.0647256742
38699.5563685481
52310.578005926
-66729.7609269372
y =
-464698.41308258
316839.903892009
298087.560112334
-499661.758015527
298806.606131584
129251.742701853
256843.382855648
-367726.4174525
-566816.147075169
320336.240040152
278737.299892196
z =
266.712
37.838
7.79
301.42
145.949
145.305
112.418
289.788
268.437
-10.679
201.505
The X and Y values are grid coordinates. The Z values are ellipsoidal heights in metres.
I tried using the following code:
contour(x,y,z)
And I get this error:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
Error in Contours (line 284)
contour(x,y,z)
Can anyone help me create a contour map with contour lines every 10m?

Star Strider on 18 May 2020
This is likely the best you can do:
xv = linspace(min(x), max(x), numel(x));
yv = linspace(min(y), max(y), numel(y));
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(x, y, z, Xm, Ym);
figure
contourf(Xm, Ym, Zm)
grid
This creates monotonically-increading vectors from the original ‘x’ and ‘y’ vectors using linspace, creates corresponding matrices using ndgrid, then interpolates them using griddata to create the matrices necessary for the contour function. (The grid call is optional.)
Star Strider on 31 May 2020
If I remember correctly, the blank areas are because griddata is extrapolating, and except for the 'v4' method, extrapolated values are NaN and so don’t plot. Experiment with 'v4' to see if it does what you want. See the documentation section on method for details.
If there is a Mapping Toolbox function that approximates this, it may be preferable to griddata. I don’t have the Mapping Toolbox, so I can’t determine that.

KSSV on 18 May 2020
Wesley Brown on 19 May 2020
I found this link very confusing. I like the way tricontour looks but I can't figure it out.