Asked by Shakir Hussain
on 26 Jun 2018

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

Answer by KSSV
on 26 Jun 2018

Edited by KSSV
on 26 Jun 2018

Accepted Answer

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

Shakir Hussain
on 18 Aug 2018

KSSV
on 18 Aug 2018

What is i? I think i is going beyond length(S).

Shakir Hussain
on 19 Aug 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## KSSV (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/407463-how-to-subset-the-matrix-array-with-shape-file#comment_582676

## Shakir Hussain (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/407463-how-to-subset-the-matrix-array-with-shape-file#comment_582679

## KSSV (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/407463-how-to-subset-the-matrix-array-with-shape-file#comment_582684

Sign in to comment.