How to plot a 3-column matrix as a color map in MATLAB?

jeff picture jeff · Mar 26, 2014 · Viewed 22.9k times · Source

I have a matrix containing the temperature value for a set of GPS coordinates. So my matrix looks like this :

Longitude  Latitude  Value
---------  --------  -----
12.345678  23.456789   25 
12.345679  23.456790   26
%should be :
%  x           y        z

etc.

I want to convert this matrix into a human-viewable plot like a color plot (2D or 3D), how can I do this?

3D can be something like this : enter image description here

or just the 2-D version of this (looking from top z-axis).

What Have I Tried

I know MATLAB has surf and mesh functions but I cannot figure out how to use them.

If I call

surf(matrix(:,1) , matrix(:,2) , matrix(:,3));

I get the error :

Error using surf (line 75)
Z must be a matrix, not a scalar or vector

Thanks in advance for any help !

P.S : It would also be great if there is a function that "fills" the gaps by interpolation (smoothing, whatever :) ). Since I have discrete data, it would be more beautiful to represent it as a continous function.

P.S 2 : I also want to use plot_google_map in the z=0 plane.

Answer

jeff picture jeff · Mar 26, 2014

A surprisingly hard-to-find answer. But I'm lucky that somebody else has asked almost the same question here.

I'm posting the answer that worked for me :

x = matrix(:,1);
y = matrix(:,2);
z = matrix(:,3);
  xi=linspace(min(x),max(x),30)
  yi=linspace(min(y),max(y),30)
  [XI YI]=meshgrid(xi,yi);
  ZI = griddata(x,y,z,XI,YI);
  contourf(XI,YI,ZI)

which prints a nice color map.