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
See also GRDWRITE2, GRDINFO2
Kelsey Jordahl (2020). grdread2 (https://www.mathworks.com/matlabcentral/fileexchange/25683-grdread2), MATLAB Central File Exchange. Retrieved .
I'm getting the following error when I try to use grdread2:
Error using netcdf.open (line 44)
Could not open file '000-relax-up.grd'.
Error in grdread2 (line 61)
ncid = netcdf.open(file, 'NC_NOWRITE');
Error in run_viscosity (line 17)
[x,y,z] = grdread2('000-relax-up.grd');
Thank you for sharing the code Kelsey.
Same problem occurs as Konstantina, any ideas?
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?
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.
Found. I just had to convert the Z matrix to double, because it was a single precision one.
Now, it works fine.
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 netcdf.open
Error in grdread2 (line
ncid = netcdf.open(file,
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)
Strangely, the grids are successfully read with grdinfo2.
Works perfectly with a huge .grd DEM. Thanks very much for this effort.
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.
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.
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.
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.