Search code examples
matlabmatrixtransformsurface

Create surface plot in MATLAB using x,y,z array


I've got a matrix in MATLAB with three columns, giving me x, y and z values:

+----+-----+----+
| 65 | 300 | 10 |
| 65 | 500 |  5 |
| 70 | 300 |  9 |
| 70 | 500 |  4 |
| 75 | 300 |  8 |
| 75 | 500 |  3 |
+----+-----+----+

The x and y data can be made to always form a grid if necessary, but it would be advantageous if they did not have to.

I'd like to create a 3D surface with this data.

I believe that means I need to transform the data such that I have Z(x,y), like:

+-----+----+----+----+
|     | 65 | 70 | 75 |
+-----+----+----+----+
| 300 | 10 |  9 |  8 |
| 500 |  5 |  4 |  3 |
+-----+----+----+----+

At which point I think I can just call surface(Z).

What's the best way to make that happen in MATLAB?


Solution

  • You can use delaunay triangulation and plot it using trisurf (triangular surface plot) as follows.

    T = delaunay(x,y);
    trisurf(T,x,y,z);