How can I write structure array to KML and shapefile format with RGB color attribute in MATLAB R2025a?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 5 Sep 2025 at 0:00
Answered: MathWorks Support Team
on 24 Sep 2025 at 15:28
I have a structure array "S" storing multiple geometry objects of polygons. One of the fields of my structure array is "Color," which stores the color of each polygon as a 1x3 RGB vector. When I call "kmlwrite" or "shapewrite" to write the structure array, I encounter the following error:
Attribute field Color of S contains at least one value that is not a scalar.
How can I write my structure array to Keyhole Markup Language (KML) format and shapefile format with the color attribute?
Accepted Answer
MathWorks Support Team
on 5 Sep 2025 at 0:00
The error message is expected because feature attributes of a geographic struct must be either numeric scalars or character vectors. This is documented in this link (see "Attribute fields" under the section "Fields of Geographic Data Structure") geographic-data-structures.
Removing the "Color" field from your structure array will make the structure compatible with the "kmlwrite" and "shapewrite" format.
To save the color attribute, you can use the "kmlwrite" function with its Name-Value arguments "Color", "FaceColor", and "EdgeColor." These arguments may be set using a choice of different color formats (including RGB triplets).
For example, the following lines of codes first extract the color matrix from "S", remove the "Color" field from "S" and write "S" to a KML file with the color matrix.
>> clrs = reshape([S.Color],[numel(S) 3]) % extract the color matrix
>> S2 = rmfield(S,"Color"); % remove the Color field from S
>> kmlwrite("S2.kml",S2,Color=clrs); % use the Color parameter to save the color attribute
You can learn more about the Name-Value arguments for color here: kmlwrite.
Alternatively, instead of removing the "Color" field from "S," you can convert the data in the "Color" field of "S" to a character vector and then use either "kmlwrite" and "shapewrite" to write. For example:
>> S3 = S;
>> for k = 1:numel(S3)
>> S3(k).Color = mat2str(S3(k).Color);
>> end
>> shapewrite("S3.shp",S3);
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!