MATLAB Answers

Convert large xyz file into grid

121 views (last 30 days)
Yoni Verhaegen -WE-1718-
Yoni Verhaegen -WE-1718- on 21 Oct 2019
Commented: Sara on 7 Dec 2020
I have a large XYZ file (300276x3, this file includes x and y coordinates (not lat/lon, but polar stereographic) and elevation z) and I'm wondering if it would be possible to convert this into a gridded dataset (n x m matrix). The xyz file can be downloaded from:
and imported in matlab by:
AIS_SEC = importdata('AIS_SEC.xyz');
I tried:
X= XYZ(:,1);
Y= XYZ(:,2);
Z= XYZ(:,3);
xr = sort(unique(X));
yr = sort(unique(Y));
gRho = zeros(length(yr),length(xr));
gRho = griddata(X,Y,Z,xr,yr')
imagesc(gRho)
Requested 300276x300276 (671.8GB) array exceeds maximum array size preference. Creation of arrays
greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size
limit or preference panel for more information.
I tried:
% Get coordinate vectors
x = unique(XYZ(:,1)) ;
y = unique(XYZ(:,2)) ;
% dimensions of the data
nx = length(x) ;
ny = length(y) ;
% Frame matrix of grid
D = reshape(XYZ(:,3),[ny,nx]) ;
% flip matrix to adjust for plot
H = flipud(H) ;
% Transpose the matrix
H = H' ; % Check if is required
surf(x,y,H) ;
Error using reshape
To RESHAPE the number of elements must not change.
I can now plot the nx3 file with scatter3 (see image)
scatter3(XYZ(:,1),XYZ(:,2),XYZ(:,3),2,XYZ(:,3)) ;
colorbar
But I'd like to do it with imagesc. Hence, I would like to convert the nx3 file into a nxm matrix (in raster/gridded format) and as en extra I would like it as a geotiff file for use in QGIS.
Thanks!

  3 Comments

Dave Eccles
Dave Eccles on 1 Sep 2020
Were you able to find a solution as to how to do this? Also have a similar problem - would also eventually like to generate a geotiff! :) Any help would be much appreciated.
Chad Greene
Chad Greene on 26 Sep 2020
Have you seen my xyz2grid function? I think it'll do what you want.
Sara
Sara on 7 Dec 2020
Hi, where is the function? i did not see it in the MATLAB helo. I also I did net know where I can the download

Sign in to comment.

Answers (2)

Bibhu Das
Bibhu Das on 21 Oct 2019
If its a 2D data try to figure out min. and max. of X and Y. Then reshape the z column so that size z is equal to 300276. Finally you can contour it.

  0 Comments

Sign in to comment.


Chad Greene
Chad Greene on 26 Sep 2020
Try my xyz2grid function. It's fast, easy to use, and one of the examples in that documentation link shows how to grid up an Antarctic dataset just like yours.

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by