How to combine 3 xyz data files

5 views (last 30 days)
Hi,
I have 3 xyz data files (each contains longitude(z), latitude(y), and the water depth(z)). For my assignments, I have to combine these 3 xyz data files together to create a new bathymetry. I have tried the following codes, but matlab just crashed everytime i run it.
%%COMBINE 2014 XYZ DATA FILES
clear all; clc
a=load('bs_09092014_soundings-mean.pts');
b=load('bs_09092014_soundings_shoalest.pts');
c=load('bs_10092014-levels.pts');
% FIND THE MIN AND MAX AMONG THE 3 DATA FILES
% FIND X MIN
if ((min(a(:,1))>min(b(:,1))) && (min(a(:,1))>min(c(:,1))))
xmin=min(a(:,1));
else
if ((min(b(:,1))>min(a(:,1))) && (min(b(:,1))>min(c(:,1))))
xmin=min(b(:,1));
else
xmin=min(c(:,1));
end
end
% FIND X MAX
if ((max(a(:,1))<max(b(:,1))) && (max(a(:,1))<max(c(:,1))))
xmax=max(a(:,1));
else
if ((max(b(:,1))<max(a(:,1))) && (max(b(:,1))<max(c(:,1))))
xmax=max(b(:,1));
else
xmax=max(c(:,1));
end
end
% FIND Y MIN
if ((min(a(:,2))>min(b(:,2))) && (min(a(:,2))>min(c(:,2))))
ymin=min(a(:,2));
else
if ((min(b(:,2))>min(a(:,2))) && (min(b(:,2))>min(c(:,2))))
ymin=min(b(:,2));
else
ymin=min(c(:,2));
end
end
% FIND Y MAX
if ((max(a(:,2))<max(b(:,2))) && (max(a(:,2))>max(c(:,2))))
ymax=max(a(:,2));
else
if ((max(b(:,2))>max(a(:,2))) && (max(b(:,2))>max(c(:,2))))
ymax=max(b(:,2));
else
ymax=max(c(:,2));
end
end
% FIND THE INDICES BETWEEN XY MIN AND MAX, TO CREATE A NEW MATRIX THAT CONSISTS OF THE INTERSECTIONAL AREA OF ALL 3 XYZ FILES
aInd=find(a(:,1)>xmin & a(:,1)<xmax & a(:,2)>ymin & a(:,2)<ymax);
xa=a(aInd,1);
ya=a(aInd,2);
za=a(aInd,3);
bInd=find(b(:,1)>xmin & b(:,1)<xmax & b(:,2)>ymin & b(:,2)<ymax);
xb=b(bInd,1);
yb=b(bInd,2);
zb=b(bInd,3);
cInd=find(c(:,1)>xmin & c(:,1)<xmax & c(:,2)>ymin & c(:,2)<ymax);
xc=c(cInd,1);
yc=c(cInd,2);
zc=c(cInd,3);
xstep=linspace(xmin,xmax,1000);
ystep=linspace(ymin,ymax,1000);
[xia yia]=meshgrid(xstep,ystep);
zia=griddata(xa,ya,za,xia,yia);
[xib yib]=meshgrid(xstep,ystep);
zib=griddata(xb,yb,zb,xib,yib);
[xic yic]=meshgrid(xstep,ystep)'
zic=griddata(xc,yc,zc,xic,yic);
pcolor(xia,yia,zia);
shading interp
hold on
pcolor(xib,yib,zib);
shading interp
hold on
pcolor(xic,yic,zic);
shading interp
hold off
Thanks, Eve

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 20 Oct 2014
Edited: Mohammad Abouali on 20 Oct 2014
Follow this procedure:
1) load your three files into a,b,c variable
2) combine them into one variable:
x=[a(:,1),b(:,1),c(:,1)];
y=[a(:,2),b(:,2),c(:,2)];
z=[a(:,3),b(:,3),c(:,3)];
3) make a scattered intrpolant
F = scatteredInterpolant(x,y,z)
4) make a gridded region with proper spacing of your choice
minX=min(x);
maxX=max(x);
same goes for y; then make a regularly spaced grid:
[XGrid, YGrid]=ndgrid(minX:dx:maxX,minX:dx:maxX);
5) interpolate your data back to this area grid by calling
zGrid=F(XGrid,YGrid);
  8 Comments
Charlotte Findlay
Charlotte Findlay on 2 May 2019
Could I ask what the 'dx' component of this part of the code would be?
[XGrid, YGrid]=ndgrid(minX:dx:maxX,minX:dx:maxX);

Sign in to comment.

More Answers (1)

j.D
j.D on 4 May 2018
Can this code work for two and what changes are needed if it can be used for two

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!