O'Reilly logo

MATLAB Graphics and Data Visualization Cookbook by Swapnonil Banerjee, Nivedita Majumdar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Gridding scattered data

If the two-dimensional data does not exist on a uniform grid, methods such as surf will not be applicable. MATLAB provides functionality to fit scatter points with a uniform grid and thereby facilitate the use of the standard techniques to 2D visualization on such data, as shown in this recipe.

Getting ready

Generate a scattered dataset in 2D space:

load griddataExample;
R = sqrt(x.^2 + y.^2) + eps; z = sin(R)./R;

How to do it…

Perform the following steps:

  1. Define a uniform grid:
    xx = linspace(min(x),max(x),30);
    yy = linspace(min(y),max(y),30);
    [X,Y] = meshgrid(xx,yy);
  2. Fit a surface to the scattered data using MATLAB command griddata:
    Z_griddata = griddata(x,y,z,X,Y);
  3. An alternate method is using MATLAB command triscatteredinterp ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required