MATLAB: Do I receive an error when I use GRADIENTM function in Mapping Toolbox 3.0 (R2009b)

Mapping Toolbox

I am using the GRADIENTM function to calculate the gradient of a data grid. The data grid is read from a TIFF file whereas the referencing vector is extracted from a TFW worldfile:
[X, cmap] = imread('concord_ortho_w.tif');
refvec = worldfileread('concord_ortho_w.tfw');
[aspect,slope,gradN,gradE] = gradientm(X,refvec)
but I receive the following error:
??? Error using ==> convertToGeoRasterRef at
71
In combination with the number of rows in the
raster grid, 2000, the input referencing
vector or matrix implies latitude limits that
extend outside the interval [-90 90] degrees.
Error in ==> gradientm at 120
R = convertToGeoRasterRef(R, size(map),
'degrees', mfilename, 'R', 2);

Best Answer

This error can occur if the regular data grid referenced in the worldfile is in some projected coordinate system, and not a geographic (latitude-longitude) system. GRADIENTM is not applicable in this case. Instead, you should use the GRADIENT function, which is part of base MATLAB:
[dx,dy] = gradient(dtm);
and scale the first return value by R(2,1) - the width of a raster cell in the x-direction -- and the second by R(1,2) --the (signed) height of a raster cell in the y-direction.
dx = dx / R(2,1);
dy = dy / R(1,2);
This will convert the gradient arrays from change per column or row to change per x and y in the projected system.
For more information on the GRADIENT function, execute the following in the MATLAB command prompt:
doc gradient