How to use fill or patch when some vertices are NaN?

24 views (last 30 days)
Hi All,
I have a structure of water body outlines read in from a shapefile defining 1000+ water bodies. Each element of the structure contains (usually) one but sometimes more water bodies with the last row of each body being NaN. Both Patch and Fill plot only the outlines and won't put in the color because of the nan. For some of them I can just patch/fill values from 1:end-1 and that works, but when there are two water bodies with a NaN in the middle. How can I patch/fill these water bodies on a regular plot axes (not a map axes)?
water(1)
ans =
Geometry: 'Polygon'
BoundingBox: [2x2 double]
X: [3.0289e+05 3.0294e+05 3.0299e+05 3.03e+05 3.0298e+05 3.0292e+05 3.0292e+05 3.029e+05 3.0289e+05 NaN]
Y: [4.2084e+06 4.2084e+06 4.2084e+06 4.2084e+06 4.2083e+06 4.2083e+06 4.2084e+06 4.2084e+06 4.2084e+06 NaN]
fill(water(i).Lon(1:end-1), water(i).Lat(1:end-1),[0.5 0.5 1],'FaceColor',[0.5 0.5 1]) % Works and plots with color
fill(water(i).Lon, water(i).Lat,[0.5 0.5 1],'FaceColor',[0.5 0.5 1]) % Plots only the outline
water(8)
ans =
Geometry: 'Polygon'
BoundingBox: [2x2 double]
X: [1x89 double]
Y: [1x89 double]
water(8).X
ans =
Columns 1 through 10
3.4428e+05 3.4427e+05 3.4427e+05 3.4429e+05 3.4427e+05 3.4425e+05 3.4424e+05 3.4424e+05 3.4425e+05 3.4425e+05
Columns 11 though 20
3.4428e+05 NaN 3.4419e+05 3.4419e+05 3.442e+05 3.442e+05 3.442e+05 3.4418e+05 3.4416e+05 3.4415e+05
Columns 21 through 30
3.4413e+05 3.441e+05 3.4408e+05 3.4407e+05 3.4409e+05 3.441e+05 3.4412e+05 3.4413e+05 3.4415e+05 3.4417e+05
Columns 31 through 40
3.4419e+05 NaN % Etc etc.
i = 8; fill(water(i).Lon(1:end-1), water(i).Lat(1:end-1),[0.5 0.5 1],'FaceColor',[0.5 0.5 1]) % Fails because of NaNs in the vectors
  2 Comments
Vaidyanathan Thiagarajan
Vaidyanathan Thiagarajan on 28 Aug 2017
Hello Emily,
You can remove the NaNs and then use 'fill' as shown in the following code snippet :
XY = [0 0; 1 0; 1 1; NaN NaN;0.5 0.25];
X = XY(:,1);
Y = XY(:,2);
%remove all NaN's from both X and Y
X = X(~isnan(X));
Y = Y(~isnan(Y));
fill(X,Y,[0.5 0.5 1],'FaceColor',[0.5 0.5 1])
The above code assumes that there is a one-to-one mapping of NaNs in X and Y. If that is not the case, you might have to suitably modify the code. For more details on 'isnan' and not (~) function, kindly refer to the following links :
However, it is best to give some thought about why you are getting NaNs in the first place. It suggests a possible problem in the underlying algorithm. Hence, it would be best to address this issue first rather than explicitly removing NaNs (as in the above code).
Hope this helps.
Vaidyanathan
the cyclist
the cyclist on 28 Aug 2017
Edited: the cyclist on 28 Aug 2017
You could do
keepIndex = ~isnan(X) & ~isnan(Y);
X = X(keepIndex);
Y = Y(keepIndex);
if the NaNs do not always appear in both X and Y.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!