Handle warning of "Array is wrong shape or size" when plotting a geopolyshape and setting the ColorData field in MATLAB
8 views (last 30 days)
Show older comments
MATLAB is throwing a warning and not plotting a geopolyshape when setting the ColorData field; however the shape is plotted when ColorData isn't set. The example code below demonstrates the issue. I am using MATLAB2024b.
Note the latitude and longitude correspond to a H3 resolution 8 hexagon. I don't think the issue is because it's an H3 cell because other H3 cells plot fine with ColorData but I'm sharing for completeness.
So how can I plot this geopolyshape while setting ColorData?
lat = [41.9480823417544; ...
41.9232620388895; ...
41.9297343644414; ...
41.9610354203583; ...
41.9858827546380; ...
41.9794019931882; ...
41.9480823417544];
lon = [-71.5797574810054; ...
-71.5494225035612; ...
-71.5033771429879; ...
-71.4876121649763; ...
-71.5179362450247; ...
-71.5640362454369; ...
-71.5797574810054];
% Create geopolyshape
shape = geopolyshape(lat, lon);
figure;
tiledlayout('flow');
nexttile;
geoplot(shape,'FaceColor','b');
title('works')
nexttile;
geoplot(shape,'FaceColor','flat');
title('works')
nexttile;
geoplot(shape,'FaceColor','flat','ColorData',1);
title('fails and throws warning')
nexttile;
geoplot(shape,'ColorData',5);
title('fails and throws warning')
0 Comments
Answers (1)
Walter Roberson
on 10 Mar 2025
ColorData — Line colors
Line colors, specified as a numeric vector that linearly maps to the colors in the current colormap. The plot uses a different color for each element of ShapeData. Specify ColorData as a vector the same length as ShapeData.
So, your problem is that you are passing in scalar ColorData, when it needs to be a vector the same length as the shape data.
2 Comments
Walter Roberson
on 10 Mar 2025
Hmmm, I don't know.
I do notice however that if you duplicate the shape and use colordata length 2, there is no warning.
lat = [41.9480823417544; ...
41.9232620388895; ...
41.9297343644414; ...
41.9610354203583; ...
41.9858827546380; ...
41.9794019931882; ...
41.9480823417544];
lon = [-71.5797574810054; ...
-71.5494225035612; ...
-71.5033771429879; ...
-71.4876121649763; ...
-71.5179362450247; ...
-71.5640362454369; ...
-71.5797574810054];
% Create geopolyshape
shape = geopolyshape(lat, lon);
geoplot([shape;shape],'FaceColor','flat','ColorData',[5 5]);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
