Finding the closest coordinate from a surface plot based on a X, Y location
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jorge Luis Paredes Estacio
il 16 Ago 2024
Commentato: Voss
il 20 Ago 2024
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
0 Commenti
Risposta accettata
Voss
il 16 Ago 2024
Spostato: Torsten
il 16 Ago 2024
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 Commenti
Più risposte (2)
Torsten
il 16 Ago 2024
Modificato: Torsten
il 16 Ago 2024
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 Commenti
Torsten
il 16 Ago 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.
Matt J
il 16 Ago 2024
Modificato: Matt J
il 16 Ago 2024
F=griddedInterpolant({Slab_strike.x,Slab_strike.y},Slab_strike.z,'linear','nearest');
Strike = F(lon_GMM,lat_GMM);
Vedere anche
Categorie
Scopri di più su Geographic Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!