File Exchange

image thumbnail


version (1.95 KB) by James Slegers
Import a .kml file as a series of shapefile structs. Like kml_shapefile, but more stable.


Updated 08 Jun 2012

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Import a Google Earth .kml file of points, lines, polygons, or a combination, as a Matlab struct, compatible with the Mapping Toolbox features, and with shapefiles imported with shaperead().

Structs are composed of the following fields:

- Geometry
- Name
- Description
- Lon
- Lat
- BoundingBox

Mapping Toolbox polygons require a clockwise convention to specify between negative and positive areas. These are not specified in .kml files, so all polygons are converted to clockwise orientation to guarantee that they represent positive space.

Cite As

James Slegers (2021). kml2struct (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)

Rachid Belaroussi


Reno Filla

Please note that the fix proposed by Davide Lovell bears a fatal flaw: what about multiples of both 2 and 3? If you have 12 values, how would you know if it's 4x (lat, lon, alt) or 6x (lat, lon)? Sure, you could compare all lat-values and all lon-values and reason about similarity but one should never add too much intelligence into basic functions. In my version I handle this with an optional parameter. (It also read KMZ files, not only KML). Published at

Reno Filla

This can only import one geometry per placemark. I therefore modified the code and published it here:

Marc Compere

requires Mapping Toolbox. No workee

Jessica Haskins

Davide Lovell's comment on this file is pretty necessary if the KML file you're loading in only has 2-D geometry. Inserting that at line 77 allowed the code to run without error on my 2-D only data.

Amanda Wild

The description contains a height value for each point after the latitude and longitude. Then there is an east and north component before another height is listed. Which height, the first one or the second is the altitude? The two height values for one point are different. What are they representing and why are they different?

Dennis Gardner

Golam Nayeem

Can you help me to make 2D matrix representation of KML file to plot 3D surface?

Thanks so much! your function works so good!

Collin Walker

Nikolaus Houben

When I put in the kml file name as the argument it tells me it needs more arguments. Why?

Joel Rowland

I get an error when loading a kml file generated via Google Earth Engine API. The kml only contains points, no lines or polygons. Also as mentioned by another, you will need the Mapping Toolbox for this to work.

David Lovell

I came across a kml file that had only 2-D geometry, and that generated with this file. This can be fixed by counting commas, however. The following code can be substituted in the relevant part of this file:

numOfCommas = length(strfind(coordStr, ','));
[m,n] = size(coordMat);
if (m*n/numOfCommas == 2)
% Rearrange coordinates to form an x-by-2 matrix
coordMat = reshape(coordMat,2,m*n/2)';
% Rearrange coordinates to form an x-by-3 matrix
coordMat = reshape(coordMat,3,m*n/3)';

Bruno Dias

Once I wrote a function that would allow me to read files generated by Google Earth and get the names and coordinates of points or paths contained in that file. However, these files had to be generated by Google Earth. This m-file enabled me to overcome this problem.
Thank you.

Bruno Dias

However, these files had to be generated by Google Earth. This m-file enabled me to overcome this problem.
Thank you.

Chad Greene

This function solved my problem perfectly, and it's easy to use. Thanks for sharing.

Lex Lombardi

Found a bug with the regular expression used to match XML fields. '.+?' is used to match the value between an xml tag pair, but this does not match the empty case. '.*?' will match an empty tag pair.


Does just what I need with no significant effort on my part - Thanks

David Young

Repeating Brian Emery's point: it's important that the "Required Products" list includes the mapping toolbox. At the time of writing this comment, it's not listed as required.

John Paden

Brian Emery

Note that this requires poly2ccw.m which is in the mapping toolbox.

Joao Luiz Vieira


This works well, but it has a couple minor bugs.
1. The "cell2double" function on line 77 should be replaced with "str2double".
2. The regular expressions on lines 44-45 fail to trim off the <description> tags.


MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!