Code covered by the BSD License  

Highlights from

4.0 | 2 ratings Rate this file 78 Downloads (last 30 days) File Size: 2.69 KB File ID: #25683 Version: 1.2



Kelsey Jordahl (view profile)


29 Oct 2009 (Updated )

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

| Watch this File

File Information

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



This file inspired Grdinfo2 and Grdwrite2.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
17 Sep 2015 Marcia Maia

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

Comment only
17 Sep 2015 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)...

Comment only
16 Sep 2015 Konstantina

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?

Comment only
16 Sep 2015 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?

Comment only
30 Jan 2013 Fran├žois Beauducel

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

31 Mar 2012 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.

Comment only
27 Mar 2012 David

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.


01 Dec 2009 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.

Comment only
01 Dec 2009 Paul

Paul (view profile)

doesn't work on R2009b

Comment only
03 Dec 2009 1.1

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

06 Jan 2010 1.2

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

Contact us