The nature of your data is a bit unclear. You say scattered data but that the hole is associated with a value of zero. If you have coordinates for the hole, then Id just extract those, define a polygon around the hole using boundary and remove all points inside the polygon after interpolation using inpolygon . Some untested code:
xh=rawX((rawZ==0)):
yh=rawY((rawZ==0)):
p=boundary(xh,yh);
[X,Y]=meshgrid(resx,resy);
Z=griddata(rawX,rawY,rawZ,X,Y)
in=inpolygon(X,Y,xh(p),yh(p))
Z(in)=NaN;
surf(X,Y,Z)
Thats simple. The following two solutions are for when the holes are defined by their lack of data:
Technically, there are gaps between every one of your data points and you want to remove large gaps. There is, to my knowledge, no option in griddata to define the "largest distance" over which it is allowed to interpolate. Here are however two options for you where you first interpolate and then remove the data where the holes were.
Method 1:
Create a grid for interpolation and find the closest distance between each point in your grid and a data point using dsearchn .
x=rand(1,1e4)';
y=rand(1,1e4)';
z=x.*y;
xp=[0.4 0.6 0.6 0.4 0.4];
yp=[0.4 0.4 0.6 0.6 0.4];
in=inpolygon(x,y,xp,yp);
x(in)=[];
y(in)=[];
z(in)=[];
subplot(1,3,1)
plot(x,y,'.')
[X,Y]=meshgrid(0:0.01:1,0:0.01:1);
[a b]=dsearchn([x,y],[X(:),Y(:)])
subplot(1,3,2)
scatter(X(:),Y(:),[],b)
id=a(b>.02)
You clearly see the hole in the right figure. Now you can just grab the indices (using some threshold value) and set those points to NaN after interpolating onto your grid using griddata.
Method 2 (faster)
Use hist3 to find bins with missing data. Define a polygon around those points and remove points inside of the polygon.
[X,Y]=meshgrid(0:0.01:1,0:0.01:1);
Z = griddata(x,y,z,X,Y);
[count,bins]=hist3([x,y],'ctrs',{0:0.03:1,0:0.03:1},'cdatamode','auto');
bins=[bins{1}(:) bins{2}(:)];
[idx,idy]=find(count==0);
xh=bins(idx,2);
yh=bins(idy,1);
p=boundary(xh,yh);
subplot(1,4,2)
plot(x,y,'.',...
xh(p),yh(p),'r');
in=inpolygon(X,Y,xh(p),yh(p))
Z(in)=NaN;
subplot(1,4,3)
surf(X,Y,Z,'edgecolor','none')
Note that the hole is not completely filled. You can fill a larger portion of the hole by reducing the bin size. However, with smaller bins you may also get other bins with holes, depending on the nature of your data. If this happens, then you have to remove outliers before running boundary, else you will get a very oddly shaped polygon.
Best Answer