How to blank a grid value with a boundary file?

1 visualización (últimos 30 días)
Leon
Leon el 21 de Abr. de 2021
Comentada: Leon el 21 de Abr. de 2021
I have a grid like the below:
[X, Y] = ndgrid([-180:0.25:179.75],[-90:0.25:89.75]);
Z = griddata(...); %on the same grid as X and Y
I also have a boundary file like the below (a lot more complicated in reality):
140 -5
140 -30
160 -30
160 -5
140 -5
How do I use the boundary file as above to only get the griddata (X1, Y1 and Z1) within the polygon and plot them onto a map as a contour like the below?
[C1, h1] = contourf (X1, Y1, Z1);

Respuesta aceptada

Clayton Gotberg
Clayton Gotberg el 21 de Abr. de 2021
Use the inpolygon function to determine which points are inside the boundary you define, then replace those points which are outside with NaN.
[X, Y] = ndgrid([-180:0.25:179.75],[-90:0.25:89.75]);
Z = griddata(...); %on the same grid as X and Y
boundaries = readmatrix('boundary_file') % replace with actual boundary file
% may need to change file-reading function
boundary_x = boundaries(:,1);% x values of boundary file
boundary_y = boundaries(:,2);% y values of boundary file
in_bounds = inpolygon(X,Y,boundary_x,boundary_y);
X(~in_bounds) = NaN;
Y(~in_bounds) = NaN;
Z(~in_bounds) = NaN;
If it's too slow, there's also the inpoly package in the file exchange.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by