How to blank a grid value with a boundary file?

조회 수: 1 (최근 30일)
Leon
Leon 2021년 4월 21일
댓글: Leon 2021년 4월 21일
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개)

카테고리

Help CenterFile Exchange에서 Geometric Geodesy에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by