MATLAB: How to create a tiff file from a nx3 matrix

tiff

I have data from an nx3 matrix where column 1 is the x value, column 2 is the y value, and column 3 is an attribute value. Is it possible to create a tiff file using this information? I've been trying to use the Tiff command but I don't think I am setting up tagstruct correctly, because I continue to get errors when I try to write. Thanks.

Best Answer

Let data be your n*3 matrix.
x = data(:,1) ;
y = data(:,2) ;
z = data(:,3) ;
% convert the data to grid
nx = 500 ; ny = 500 ;
xi = linspace(min(x),max(x),nx) ;
yi = linspace(min(y),max(y),ny) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
% write to tiff file
lonmin = min(x); lonmax = max(x);
latmin = min(y); latmax = max(y);
% Write to geotiff
R = georasterref('RasterSize',size(Z),'LatitudeLimits',[latmin,latmax],'LongitudeLimits',[lonmin,lonmax]);
tiffile = strcat('myFile','.tif') ;
geotiffwrite(tiffile,Z,R)