How to blank a grid value with a boundary file?

1 view (last 30 days)
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);

Accepted Answer

Clayton Gotberg
Clayton Gotberg on 21 Apr 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.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!