# How to assign corresponding Z value to gridded X,Y data ?

조회 수: 44(최근 30일)
Mustafa Alper Cetintas 2021년 7월 6일
댓글: Scott MacKenzie 2021년 7월 7일
Hello all,
Having a problem with interpolating the data. I have a live data stream project where data comes from the serial port. After several steps, I want to create a XYZ grid from easting,northing and depth values.
Currently, script creates a surface in real time with "fit" function. 3D surface is visible in the figure and it is updating, so I reckon I have my interpolated surface. Then creating a meshgrid [X,Y] at interested grid size successfully. Then I tried to use "interp2" function to interpolate Z values at grid coordinates(created in meshgrid). But it's not working. Shared the error below as well. How can the matlab produce Z grid corresponding to the X Y grid (which is produced in meshgrid) ?
Kind Regards,
Alper
drawT=0;
while true
...
lla = [lla; Easting, Northing, Depth];
HRZntl=[lla(:,1) lla(:,2)];
if drawT == 10 % once every 10 input data
figure(1)
clf
[X, Y]=meshgrid(min(lla(:,1)):0.1:max(lla(:,1)), min(lla(:,2)):0.1:max(lla(:,2))); %Gridding the data at .1 meters
fitObj=fit(HRZntl,lla(:,3),'linearinterp'); % fitting the surface to my Easting, Northing and Depth data and it works
Z=interp2(lla(:,1),lla(:,2),lla(:,3),X,Y); % Giving Error here
plot(fitObj,HRZntl,lla(:,3));
drawT=0;
axis tight
grid on
end
end
Once this code runs, I receive the error below;
Error using griddedInterpolant
The number of input coordinate arrays must match the dimensions of the sample values.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 126)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in RealTime_SBES_DataCollection_and_Mapping (line 108)
Z=interp2(lla(:,1),lla(:,2),lla(:,3),X,Y);

댓글을 달려면 로그인하십시오.

### 채택된 답변

Scott MacKenzie 2021년 7월 7일
Try replacing interp2 with griddata, like this:
[X,Y] = meshgrid(min(lla(:,1)):0.1:max(lla(:,1)), min(lla(:,2)):0.1:max(lla(:,2)));
Z = griddata(lla(:,1),lla(:,2),lla(:,3),X,Y);
If the original data are in x, y, and z vectors, as is the case here, then griddata is the best choice for interpolation.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Scott MacKenzie 2021년 7월 7일
@Mustafa Alper Cetintas You're welcome. Glad to help. Good luck with your research.

댓글을 달려면 로그인하십시오.

R2020b

### Community Treasure Hunt

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

Start Hunting!