필터 지우기
필터 지우기

Finding the closest coordinate from a surface plot based on a X, Y location

조회 수: 86 (최근 30일)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2024년 8월 16일 16:48
댓글: Voss 2024년 8월 20일 14:26
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)
Strike = NaN
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

채택된 답변

Voss
Voss 2024년 8월 16일 18:17
이동: Torsten 2024년 8월 16일 21:11
load slab_strike
is_ok = ~isnan(z);
[X,Y] = meshgrid(x,y);
X = X(is_ok);
Y = Y(is_ok);
Z = z(is_ok);
lon_GMM = -73.5295;
lat_GMM = -17.8990;
[~,idx] = min((X-lon_GMM).^2+(Y-lat_GMM).^2);
figure()
hold on
surf(x,y,z,'EdgeColor','none')
h1 = plot(lon_GMM,lat_GMM,'.r');
h2 = plot3(X(idx),Y(idx),Z(idx),'.k');
legend([h1 h2],{'Requested Point','Nearest Non-NaN z'})
xlim(lon_GMM+[-5 5])
ylim(lat_GMM+[-5 5])
  댓글 수: 6
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2024년 8월 20일 13:02
편집: Jorge Luis Paredes Estacio 2024년 8월 20일 13:02
I got it now. Done. Thank you :).

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

추가 답변 (2개)

Torsten
Torsten 2024년 8월 16일 17:08
편집: Torsten 2024년 8월 16일 17:22
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
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2024년 8월 16일 17:50
편집: Jorge Luis Paredes Estacio 2024년 8월 16일 17:52
Thank you for your replied. That is correct, the selection should be based of finding the closest point withouth having a NaN value in the Z coordinate. A NaN value won't be useful for my analysis. In other words, it should skip that coordinate and choose another until it provides a non-NaN value.
Torsten
Torsten 2024년 8월 16일 17:56
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.

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


Matt J
Matt J 2024년 8월 16일 17:15
편집: Matt J 2024년 8월 16일 17:19
F=griddedInterpolant({Slab_strike.x,Slab_strike.y},Slab_strike.z,'linear','nearest');
Strike = F(lon_GMM,lat_GMM);
  댓글 수: 6
Torsten
Torsten 2024년 8월 16일 17:37
편집: Torsten 2024년 8월 16일 17:38
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
Jorge Luis Paredes Estacio 2024년 8월 16일 17:45
I still have the issue in selecting the closest point and avoiding as the same time the selection of any NaN value if that is the case. Thank you for your help.

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

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by