Finding the closest coordinate from a surface plot based on a X, Y location
Show older comments
Hello, I want to extrapolate a point (longitude,latitude) from the coordinates of a surface file (attached here as "slab_strike") as it is empty (Strike=NaN) when using interp2 as the points are outside the boundary. Despite I used the option "nearest", it is empty anyway.
% Coordinates of the points:
lat_GMM= -17.8990;
lon_GMM=-73.5295;
% The surface plot
load slab_strike % Loading the slab strike
Slab_strike.x=x;
Slab_strike.y=y;
Slab_strike.z=z;
Strike = interp2(Slab_strike.x,Slab_strike.y,Slab_strike.z,lon_GMM,lat_GMM)
As Strike=NaN, there is a way I can choose the closest point value from the surface avoiding any NaN value and select the closest non-NaN value.
I would appreciate the help
Accepted Answer
More Answers (2)
lat_GMM and lon_GMM are not within the rectangle in which data for z are supplied.
In this case, interp2 returns NaN because it does not extrapolate.
Choose lat_GMM and lon_GMM in the limits for x and y where values are given for z.
Else you could use
[~,idx] = min(abs(x-lon_GMM))
[~,idy] = min(abs(y-lat_GMM))
Strike = z(idy,idx)
But it seems that your z-matrix contains NaN values.
6 Comments
Jorge Luis Paredes Estacio
on 16 Aug 2024
Torsten
on 16 Aug 2024
The three lines of code should answer your question.
Jorge Luis Paredes Estacio
on 16 Aug 2024
I don't know what you mean. The z-value in the point nearest to your lat and lon values is NaN.
% Coordinates of the points:
lat_GMM= -17.8990;
lon_GMM= -73.5295;
% The surface plot
load slab_strike % Loading the slab strike
Slab_strike.x=x;
Slab_strike.y=y;
Slab_strike.z=z;
[~,idx] = min(abs(x-lon_GMM));
x(idx)
[~,idy] = min(abs(y-lat_GMM));
y(idy)
Strike = z(idy,idx)
Jorge Luis Paredes Estacio
on 16 Aug 2024
Edited: Jorge Luis Paredes Estacio
on 16 Aug 2024
Torsten
on 16 Aug 2024
Then try whether z is NaN in the 8 points surrounding (x(idx),y(idy)).
If this also doesn't work, test z for NaN in the 16 following points and so on.
F=griddedInterpolant({Slab_strike.x,Slab_strike.y},Slab_strike.z,'linear','nearest');
Strike = F(lon_GMM,lat_GMM);
6 Comments
Torsten
on 16 Aug 2024
Most probably
F=griddedInterpolant({Slab_strike.x,Slab_strike.y},Slab_strike.z.','linear','nearest');
instead of
F=griddedInterpolant({Slab_strike.x,Slab_strike.y},Slab_strike.z,'linear','nearest');
Jorge Luis Paredes Estacio
on 16 Aug 2024
Jorge Luis Paredes Estacio
on 16 Aug 2024
Matt J
on 16 Aug 2024
I assume the issue is resolved, since you accept-clicked Torsten's answer?
Thank you. The error dissapeared, but Strike provides a NaN value
Strike=NaN;
The z-value nearest to your latitude and longitude is NaN - the result you also got using interp2.
Jorge Luis Paredes Estacio
on 16 Aug 2024
Categories
Find more on Mathematics 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!