MATLAB: Fill inside a polygon with a color

figureMATLABplot

Dear all,
I want to fill a polygon that I readed from shapefile with blue in order to visualzing it as lake. I searched alot and tried:
S2 = shaperead('lake.shp');
polygonwater1_y = S2(1).X;
polygonwater1_x = S2(1).Y;
fill (polygonwater1_y,polygonwater1_x,'b')
And:
patch (polygonwater1_y,polygonwater1_x,'b')
But as you can see below, unfortunately, it doesn't fill:
Do you know how can I do to fill it?
Thank you

Best Answer

It happned because, your region have multiple regions inside and they have NaN's.
shfile = "Lakes/lakes.shp" ;
S = shaperead(shfile) ;
N = length(S) ;
x = S(3).X ; y = S(3).Y ; % Picked the 3rd region
idx = find(isnan(x)) ; % find positions of NaNs
idx = [1 idx length(x)] ; % append first and last position
figure
hold on
for i = 1:length(idx)-1
pos = idx(i):idx(i+1) ; % gEt the required position
xi = x(pos) ; yi = y(pos) ; % GEt the corodinates
% Remove NaN's
xi(isnan(xi)) = [] ;
yi(isnan(yi)) = [] ;
patch(xi,yi,'b') ; % color the area
end