How to blank a grid value with a boundary file?

1 次查看(过去 30 天)
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);

采纳的回答

Clayton Gotberg
Clayton Gotberg 2021-4-21
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.

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by