Extract area of matrices (lat, lon, data) by shapefile. "Requested array exceeds the maximum possible variable size."

6 ビュー (過去 30 日間)
Simone A.
Simone A. 2023 年 1 月 31 日
編集済み: Simone A. 2023 年 1 月 31 日
Hi all,
I've got 3 matrices, lat, lon, data (8000 X 8192 each) and one shapefile (80 X 1 struct, attached).
I need to extract only the lat, lon, data within the shapefile boundaries (and possibily with an extra buffer radius around it, let'say 1km beyond the boundary).
I tried with the other answers on the forum (i.e., https://it.mathworks.com/matlabcentral/answers/281717-extract-data-by-shapefile ) , or for instance using inpolygon, etc:
S=shaperead('Shape.shp');
N = length(S) ;
for i = 1:N
plot(S(i).X,S(i).Y)
hold on
end
data = %MyData 8000x8192 ;
lat = %MyLat 8000x8192;
lon = %MyLon 800x8192;
[X,Y] = meshrid(lon,lat) ;
[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
However, when I try to make a meshgrid I belive I exceed the variable size, getting this error:
Error using repmat
Requested array exceeds the maximum possible variable size.
Error in meshgrid (line 61)
xx = repmat(xrow,size(ycol));
Is there a code you could suggest to obtain the desired result, and able to deal with the size of my matrix? The shape file covers only a tiny area (geographically speaking).
Thanks in advance!

回答 (1 件)

KSSV
KSSV 2023 年 1 月 31 日
編集済み: KSSV 2023 年 1 月 31 日
You need not to use meshgrid. Already your lon, lat values are in meshgrid. Remove these lines:
lat = %MyLat 8000x8192;
lon = %MyLon 800x8192;
[X,Y] = meshrid(lon,lat) ;
So your modified code should be:
S=shaperead('Shape.shp');
N = length(S) ;
for i = 1:N
plot(S(i).X,S(i).Y)
hold on
end
data = %MyData 8000x8192 ;
lat = %MyLat 8000x8192;
lon = %MyLon 800x8192;
X = lon ;
Y = lat ;
[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
  3 件のコメント
KSSV
KSSV 2023 年 1 月 31 日
I got the question....the given code should work. Offcourse buffer is not in there.
Simone A.
Simone A. 2023 年 1 月 31 日
編集済み: Simone A. 2023 年 1 月 31 日
But the output is not a matrix, but a structure. Is there a way to obtain smaller matrices (lat, lon, data) instead? That would help a lot! thanks
Additionally, If i convert cell2mat (X = cell2mat(iwant);), I get an output where all the columns are the same:

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by