Clear Filters
Clear Filters

Clip Matrix by Polygon

5 views (last 30 days)
Suman Dhamala
Suman Dhamala on 28 Jun 2021
I have a rainfall data for whole India for 110 years (1906 to 2015). So, the rain matrix(MRainJJAS) is 135*129*110., one value for a year. This rainfall data is actually originally a part of netcdf file. The same netcdf file also contains latitude and longitude array with dimension 129*1 and 135*1 respectively.
I have a shape file(Intersect2), that is basically a part within India, and I intend to clip the rainfall matrix by the polygon, so that I only get rainfall matrix within and touched the boundaries of shapefile (Intersect2).
I tried using function clip_RasterByPolygon, which I have attached here as well.
Following is the code
ING=shaperead('Intersect2.shp'); % raster latitude % lat and long of corners are considered( not centre)
RLat=sort(latitude,'descend'); % raster longitude
RLon=longitude ;
RData=MRainJJAS;
for i=1:size(MRainJJAS,3)
PLat=ING.Y' ; % polygon latitude
PLon=ING.X' ; % polygon longitude
INGclip(:,:,i)=clip_RasterByPolygon(RData(:,:,i),RLat,RLon,PLat,PLon,'AverageByArea') ;
clear PLat PLon
end
But this everytme 'The size of RData does not match that of the RLat and RLon' error is generated. I am getting confused ,as I am begineer in MATLAB, if clip_RasterByPolygon is correct to deduce my output.
Second thing I tried following code
shapefile = 'Intersect2.shp' ;
S = shaperead(shapefile);
N = length(S);
lon = longitude;
lat = latitude;
[X,Y] = meshgrid(lon,lat) ;
data = MRainJJAS;
[nx,ny,d] = size(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
This displays error 'Index exceeds the number of array elements (1)'. I am not sure what issue is?

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!