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

30 views (last 30 days)
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);

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 7 Jul 2021
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 Comments
Mustafa Alper Cetintas
Mustafa Alper Cetintas on 7 Jul 2021
Edited: Mustafa Alper Cetintas on 7 Jul 2021
Thanks for persisting Sir! I can not be more appreciated than this for your precious help. This worked well! Attached the latest trial's screenshots here, all components of position matrice are identical-sized now, as the original data is in X,Y and Z vectors.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!