File Exchange

image thumbnail


version 1.2 (2.69 KB) by

Uses built-in netCDF capability to load a grid file created by GMT (Generic Mapping Tools) v3 or v4



View License

Z=GRDREAD2('filename.grd') will return the data as a matrix in Z

[X,Y,Z]=GRDREAD2('filename.grd') will also return X and Y vectors suitable for use in Matlab commands such as IMAGE or CONTOUR.
e.g., imagesc(X,Y,Z); axis xy

This function will load COARDS compliant netCDF grdfiles, the standard format in GMT 4 and later, as well as GMT v3 netCDF formats. The data matrix Z will have a MATLAB data type appropriate to the data type used in the netCDF file. Although both gridline and pixel registered grids can be read, pixel registration will be converted to gridline registration for the x- and y-vectors in MATLAB.

This program currently does little error checking and makes some assumptions about the content and structure of netCDF files that may not always be valid (especially in grids created in other software, even if they can be read by GMT). It has been tested on grid files created in a number of GMT versions since 3.1, using MATLAB R2009b in OS X, Linux and Windows, and in R2008b in OS X. It will not work with any binary grid file formats. It is the responsibility of the user to determine whether this program is appropriate for any given task.

For more information on GMT grid file formats, see:

GMT (Generic Mapping Tools) was developed by Paul Wessel and Walter H. F. Smith


Comments and Ratings (13)


agroh (view profile)

Thanks for the helpful routine.

It fails in case of a single output argument, since there is a probably misplaced "double" command after the "case 1" case switch. Moreover, when using variable output arguments the function definition should be [varargout]=grdread2(file) instead of [x,y,z]=grdread2(file), right?


Kurt Feigl

This routine fails for grid files that include a fourth variable named grid_mapping. Such files are written by the GMT v. 5 module named grdproject. Although a work-around solution is possible, a better solution would be to check the name of the variable.

I have the same mistake as Konstantina.

Marcia Maia

Found. I just had to convert the Z matrix to double, because it was a single precision one.
Now, it works fine.

Marcia Maia

The error I mentioned occurrs when I une Z=grdread2('filename')
When I use [X,Y,Z] I manage to read the file without error but I cannot plot it.
I get a series of messages with
"Warning: CData must be double or uint8."
It comes maybe from my grids (I tested several)...


Hi I get this error:
Error using netcdflib
The NetCDF library
encountered an error
during execution of
'open' function -
'Unknown file format

Error in
(line 60)
[varargout{:}] =
netcdflib (
varargin{1} );

Error in grdread2 (line
ncid =,

any idea?

Marcia Maia

Hi Kelsey

I tried to read grd files with your function (OS Lion and GMT 4.3.1)and got the following error messages:
Error using double
Not enough input arguments.

Error in grdread2 (line 104)
case 1,double

Strangely, the grids are successfully read with grdinfo2.
Any clue?

Works perfectly with a huge .grd DEM. Thanks very much for this effort.

Kelsey Jordahl

Kelsey Jordahl (view profile)


Could you provide more details about how it is not working? I can confirm that it still works for me on OS X 10.7.3 (Lion) and R2012a, with files created by GMT 4.5.7-1 in multiple grid formats.

grdinfo output for the .grd file and MATLAB error message would be helpful.


David (view profile)

Not working on R2011a under OS X Snow Leopard (maci64). GRD files created by GMT 4.5.7-1.

I tried replacing netcdf in your routine by ncdisp, ncread, etc. embebed on R2011a and still no success.

What a pitty! Therefore I had to use grd2xyz GMT function to read XYZ in Matlab.


Kelsey Jordahl

Kelsey Jordahl (view profile)

It works for me on R2009b, at least on Mac OS X (maci64). It doesn't seem to work any differently from R2008b for me, but I haven't tried it yet on other platforms.

Are you sure that you have a COARDS-compliant NetCDF grid generated by GMT version 4 or later? It will not work with grids generated by earlier versions of GMT, even though v3 was also NetCDF based, but it was not COARDS. I am working on an updated version of grdread2.m that will read these grids. Also, it will not necessarily work on more general COARDS-compliant NetCDF files generated by other programs, even though GMT may be able to read them.


Paul (view profile)

doesn't work on R2009b



Minor documentation changes to indicate the related files grdwrite2 and grdinfo2.


now supports all GMT netCDF grids, including those created by GMT v3.x.

MATLAB Release
MATLAB 7.7 (R2008b)

Inspired: grdinfo2, grdwrite2

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video