How to subset the matrix/array with shape file?

I have matrix of 890*751 and array 890*751*5 and I want to clip/crop/subset this matrix and array with shape file. The shape file is consist on several sub-polygon inside. Each inside sub-polygon of shape file has to clip/crop/subset separately and save with unique name. E.g, we have a shape file with 4 sub-polygon (with name (a,b,c,d) and we have to get 4 clip/crop/subset(a,b,c,d) of the matrix/array with. we can read the shape file by
area = shaperead('shapefile', 'UseGeoCoords', true);
geoshow( area , 'Facecolor', 'blue')
but no idea how to crop/clip/subset the matrix/array. The sample testmatrix, testarray and shapefile are attached here for easy to understand the question. Thank you in advance for any helpe/guide

댓글 수: 3

KSSV
KSSV 2018년 6월 26일
YOu need to provide the lat and lons for matrices.
Dear KSSV, the lat lon of matrix are attached here
KSSV
KSSV 2018년 6월 26일
편집: KSSV 2018년 6월 26일
aaah...got it....

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2018년 6월 26일
편집: KSSV 2018년 6월 26일

0 개 추천

Check this link for a similar question. You can follow the same code.
shapefile = 'shapefile.shp' ;
S = shaperead(shapefile) ;
N = length(S) ;
for i = 1:N
plot(S(i).X,S(i).Y)
hold on
end
lon = load('testlon.mat') ; lon = lon.testlon ;
lat = load('testlat.mat') ; lat = lat.testlat ;
[X,Y] = meshgrid(lon,lat) ;
data = load('testarray.mat') ; data = data.testarray ;
[nx,ny,d] = size(data) ;
%%Extract data
iwant = cell(d,N) ;
for i =1:d
A = data(:,:,i) ;
for j = 1:N
idx = inpolygon(X(:),Y(:),S(i).X,S(i).Y) ;
iwant{i,j} = A(idx) ;
end
end

댓글 수: 10

Thanks your response.
shapefile = 'AoiTp.shp';
S = shaperead(shapefile);
N = length(S);
for i = 1:N
plot(S(i).X,S(i).Y)
hold on
end
lat = testlat;
lon = testlon;
[X,Y] = meshgrid(lon,lat);
data = testarray;
[nx,ny,d] = size(data);
%%Extract data
iwant = cell(d,N);
for i =1:d
A = data(:,:,i);
for j = 1:N
idx = inpolygon(X(:),Y(:),S(i).X,S(i).Y);
iwant{i,j} = A(idx);
end
end
Why I am getting the error "Index exceeds matrix dimensions". If it work, then How we can extract the data by sub-polygon of a shape file?
The number of entries in the shapefile, N, is not definitely the same as the size of the third dimension in testarray, especially if testarray turns out to be 4 dimensional.
KSSV
KSSV 2018년 6월 28일
Which line shows error? Code worked fine with your data in my pc.
Shakir Hussain
Shakir Hussain 2018년 6월 28일
편집: Shakir Hussain 2018년 6월 28일
I think Walter is right, the third line of code (N = length(S) is equal to 5 and iwant = cell(d,N); is extracting only 5 instead of 17.
Error in ==> run at 18 idx = inpolygon(X(:),Y(:),S(i).X,S(i).Y) ;
Here is the error I am getting from the code
KSSV
KSSV 2018년 7월 6일
What error?
Error in ==> run at 18 idx = inpolygon(X(:),Y(:),S(i).X,S(i).Y) ; Here is the error I am getting from the code ( Index exceeds array bounds)
KSSV
KSSV 2018년 8월 18일
What is i? I think i is going beyond length(S).
Yes, Now it is fixed and code is working well. Last thing is how we convert the result (iwant) into 2d matrix according to the polygon extend?
Hi Shakir Hussain can you share how to convert the result (iwant) into 2d matrix according to the polygon extend?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

질문:

2018년 6월 26일

댓글:

2020년 10월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by