Kempell
Kempell

Reputation: 21

.txt XYZ coordinates into DEM - MATLAB

I have a .txt file containing x,y,z coordinates for a given landscape. I'm trying to convert it to a DEM so I can further manipulate it. So far I've been doing the conversion in ArcGIS, but Id like to do it in Matlab instead.

Here is my code:

a=load('topo_2000.txt');
x=a(:,1);
x=x(1:101);
y=a(:,2);
y=y(1:101:end);
z=a(:,3);
z_grid=reshape(z,[length(x),length(y)]);

xyz = pointCloud(z_grid);

DEM = GRIDobj(xyz);

It returns the following error messages:

Error using pointCloud/validateAndParseInputs
Invalid argument at position 1. Expected input 'xyzPoints' to be of size M-by-3 or M-by-N-by-3.

Error in pointclouds.internal.pointCloudBase (line 83)
            [xyzPoints, C, nv, I, rangeData] = this.validateAndParseInputs(varargin{:});

Error in pointCloud (line 14)
            this = [email protected](varargin{:});

Error in fromModel (line 23)
xyz = pointCloud(z_grid);

From what I've gathered this means something is wrong with how I'm creating my point cloud, but I can't figure out what exactly or how to fix it. I have tried running it without the reshaped z_grid, as follows, but that didn't work either.

Here's an example chunk of my .txt file:

0   100 0.6847
100 100 1.94
200 100 2.7184
300 100 3.2816
400 100 3.3127
500 100 3.4899
600 100 3.6633

Upvotes: 1

Views: 114

Answers (1)

Kempell
Kempell

Reputation: 21

I solved it by changing my code as follows (in case anyone else has a similar issue):

a=load('C:\full\path\to\to\file.txt');
x = a(:, 1);
y = a(:, 2);
z = a(:, 3);


[xq, yq] = meshgrid(min(x):100:max(x), min(y):100:max(y));
%with 100=my step size, i.e. the original resolution of my point grid
zq = griddata(x, y, z, xq, yq);

DEM = GRIDobj(xq, yq, zq);

Upvotes: 1

Related Questions