Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

One option that avoids unnecessarily gridding your data would be to compute the Delaunay triangulation of the scattered data points and then using a command like trisurf to plot the data. Here's an example:

N=50;
x = 2*pi*rand(N,1);
y = 2*pi*rand(N,1);
z = sin(x).*sin(y);

matrix = [x y z];

tri = delaunay(matrix(:,1),matrix(:,2));
trisurf(tri,matrix(:,1),matrix(:,2),matrix(:,3))
shading interp

Suppose your matrix is nx3. Then you can create the grid as follows:

xMin=min(myMat(:,1));
xMax=max(myMat(:,1));
yMin=min(myMat(:,2));
yMax=max(myMat(:,2));

step_x=0.5; %depends on your data

[xGrid,yGrid]=meshgrid(xMin:step_x:xMax,yMin:step_y:yMax);

Now, put your data in the third column to the appropriate indices, in the new matrix say, valMat.

You can use surf now as follows:

surf(xGrid,yGrid,valMat);

If you want interpolation, you can convolve a Gaussian kernel (maybe 3x3) with valMat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top