Main Content

geocode

Get shape object from geocoded placename

Since R2024b

Description

Use Online Provider and Installed List of Placenames

geocodedGT = geocode(name) geocodes the specified placename by converting the placename into a shape object that is associated with the placename. The output is a geospatial table that contains the placename, the shape object, and the administrative level of the placename. For more information about geocoding, see Geocoding.

The function geocodes the specified placename by using an online provider and an installed list of world regions, US states, and world cities. For more information about how geocode uses the online provider and the installed list, see Algorithms.

example

geocodedGT = geocode(name,adminlevel) specifies the administrative level for the placename.

example

Use Custom Table of Placenames

geocodedGT = geocode(name,customNamesGT) uses a custom table of placenames and shape objects instead of the online provider and installed list. This syntax searches for the placenames using the case-insensitive name table variable. If the table does not have a name table variable, then the function searches the second table variable.

example

geocodedGT = geocode(name,customNamesGT,namevar) specifies the table variable that contains the custom placenames.

example

Additional Options

geocodedGT = geocode(___,Name=Value) specifies options using one or more name-value arguments, in addition to any combination of input arguments from the previous syntaxes. For example, you can specify how to filter the results by using the FilterResults name-value argument.

Examples

collapse all

Geocode the world region of Europe. The output is a geospatial table that contains the geocoded placename, a shape object associated with the geocoded placename, and the administrative level of the geocoded placename.

geocodedGT = geocode("Europe")
geocodedGT=1×3 table
       Shape          Name      AdminLevel
    ____________    ________    __________

    geopolyshape    "Europe"     "region" 

When you geocode the name of a world region, the shape object is an area of interest (AOI) that represents either an outline or a quadrangle that approximates the region. Display the boundary of the AOI on a map. Provide more geographic context for the AOI by zooming out.

figure
geobasemap topographic
geoplot(geocodedGT,LineWidth=2,EdgeColor="#7E2F8E",FaceColor="none")
geolimits([15 75],[-54 75])

Specify placenames for the states that comprise New England: Maine, New Hampshire, Vermont, Massachusetts, Connecticut, and Rhode Island. Then, geocode the placenames. The output is a geospatial table that contains the geocoded placenames, the shape objects associated with the placenames, and the administrative levels of the placenames.

names = ["Maine","New Hampshire","Vermont","Massachusetts","Connecticut","Rhode Island"];
geocodedGT = geocode(names,"state")
geocodedGT=6×3 table
       Shape             Name          AdminLevel
    ____________    _______________    __________

    geopolyshape    "Maine"             "state"  
    geopolyshape    "New Hampshire"     "state"  
    geopolyshape    "Vermont"           "state"  
    geopolyshape    "Massachusetts"     "state"  
    geopolyshape    "Connecticut"       "state"  
    geopolyshape    "Rhode Island"      "state"  

When you geocode the names of US states, the shape objects are AOIs that represent the outlines of the states. Display the AOIs on a map.

figure
geobasemap none
geoplot(geocodedGT)

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type polygon.

Since R2025a

Geocode the placename Essex using the county administrative level. The output is geospatial table that contains several matches for the placename, including Essex County in Massachusetts.

GT1 = geocode("Essex","county")
GT1=6×3 table
       Shape                 Name              AdminLevel
    ____________    _______________________    __________

    geopolyshape    "Essex, England, GBR"       "county" 
    geopolyshape    "Essex County, MA, USA"     "county" 
    geopolyshape    "Essex County, NJ, USA"     "county" 
    geopolyshape    "Essex County, NY, USA"     "county" 
    geopolyshape    "Essex County, VT, USA"     "county" 
    geopolyshape    "Essex County, VA, USA"     "county" 

Create a new geospatial table that contains only the result for Massachusetts.

GT2 = geocode("Essex County, MA","county")
GT2=1×3 table
       Shape                 Name              AdminLevel
    ____________    _______________________    __________

    geopolyshape    "Essex County, MA, USA"     "county" 

When you geocode the name of a county, the Shape variable of the table is an AOI that approximates the region. Display the AOI on a map.

figure
geobasemap streets
geoplot(GT2)

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type polygon.

Geocode all the world cities in the installed list of placenames. Then, clip the cities to an AOI.

Get the names of world cities in the installed list by using the placenames function.

names = placenames("city");

Geocode the placenames. When you specify multiple placenames and the "city" administrative level, the geocode function searches the installed list of placenames instead of using the online provider.

geocodedGT = geocode(names,"city");

View the first eight rows of the output table. When you geocode the names of cities, the shape objects in the output table are points that represent the city locations.

head(geocodedGT)
             Shape                 Name         AdminLevel
    _______________________    _____________    __________

    ( 5.2985°N,   3.9509°W)    "Abidjan"          "city"  
    (24.6525°N,  54.7589°E)    "Abu Dhabi"        "city"  
    ( 5.6106°N,   0.2121°W)    "Accra"            "city"  
    (37.0613°N,  35.3894°E)    "Adana"            "city"  
    ( 9.0235°N,  38.7575°E)    "Addis Ababa"      "city"  
    (34.6645°S, 138.8528°E)    "Adelaide"         "city"  
    (12.8767°N,  44.5408°E)    "Aden"             "city"  
    (22.7778°N,  72.2474°E)    "Ahmadabad"        "city"  

Prepare to clip the world cities by extracting the points from the table.

points = geocodedGT.Shape;

Get an AOI that represents a bounding box for South America. Then, clip the world cities to the AOI. For more information about clipping shapes to AOIs, see Clip Vector Data to Area of Interest.

GT = geocode("South America");
aoi = GT.Shape;

clipped = geoclip(points,aoi);

Display the AOI and the clipped cities on a map.

figure
geoplot(aoi,FaceColor="none")
hold on
geoplot(clipped)

Since R2025a

Geocode the address of the MathWorks Apple Hill campus.

geocodedGT = geocode("1 Apple Hill Drive, Natick, MA","address")
geocodedGT=1×3 table
            Shape                                 Name                        AdminLevel
    ______________________    ____________________________________________    __________

    (42.2998°N, 71.3516°W)    "1 Apple Hill Drive, Natick, MA, 01760, USA"    "address" 

When you geocode an address, the output table contains a point shape in geographic coordinates. Display an icon at the coordinates stored in the point shape.

lat = geocodedGT.Shape.Latitude;
lon = geocodedGT.Shape.Longitude;
geoiconchart(lat,lon,"matlabicon.gif")

Zoom in by changing the geographic limits.

geolimits([42.2946 42.3054],[-71.3622 -71.34])

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type iconchart.

Since R2025a

Geocode the placename Lake Superior, MI using the administrative level for physical features.

geocodedGT = geocode("Lake Superior, MI","physical")
geocodedGT=1×3 table
       Shape                  Name              AdminLevel
    ____________    ________________________    __________

    geopolyshape    "Lake Superior, MI, USA"    "physical"

When you geocode the name of a physical feature, the Shape variable of the table contains an AOI that represents a quadrangle. View a map of the region by plotting the AOI with no face or edge color.

figure
geobasemap topographic
geoplot(geocodedGT,FaceColor="none",EdgeColor="none")

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type polygon.

Create a custom table of placenames by reading data from a file. Then, use the custom table to geocode a specified placename.

Create the custom table by reading data from a shapefile that contains placenames in Boston. The table stores the placenames in the NAME variable and the locations of the placenames in the Shape variable.

customNamesGT = readgeotable("boston_placenames.shp")
customNamesGT=13×4 table
            Shape                     NAME               FEATURE             COORD      
    ______________________    ____________________    _____________    _________________

    (234028.23, 900038.00)    "'BACK BAY'"            "PPL-SUBDVSN"    "422100N0710515W"
    (233569.77, 900190.06)    "BACK BAY FENS"         "MARSH"          "422105N0710535W"
    (235739.28, 901126.44)    "BEACON HILL"           "HILL"           "422135N0710400W"
    (236266.39, 900974.88)    "BOSTON"                "PPL"            "422130N0710337W"
    (235743.22, 900355.06)    "BOSTON NECK"           "PENINSULA"      "422110N0710400W"
    (234685.09, 901429.69)    "BROAD CANAL"           "CANAL"          "422145N0710446W"
    (232989.64, 901884.31)    "CAMBRIDGE"             "PPL"            "422200N0710600W"
    (236536.17, 901901.88)    "COPPS HILL"            "HILL"           "422200N0710325W"
    (234591.28, 901892.00)    "'EAST CAMBRIDGE'"      "PPL-SUBDVSN"    "422200N0710450W"
    (235741.66, 900663.62)    "FLAGSTAFF HILL"        "HILL"           "422120N0710400W"
    (237114.73, 900670.75)    "FORT POINT CHANNEL"    "CHANNEL"        "422120N0710300W"
    (235741.31, 900725.31)    "FROG POND"             "LAKE"           "422122N0710400W"
    (236548.23, 899587.88)    "SOUTH BAY"             "INLET"          "422045N0710325W"

Specify the placenames of two hills in Boston. Then, geocode the placenames by searching the custom table. By default, the geocode function searches for the placenames using the NAME variable of the custom table.

names = ["BEACON HILL","COPPS HILL"];
geocodedGT = geocode(names,customNamesGT);

View the output table. When you pass a custom table of placenames to the geocode function, the output table is a subset of the input table.

geocodedGT
geocodedGT=2×4 table
            Shape                 NAME         FEATURE          COORD      
    ______________________    _____________    _______    _________________

    (235739.28, 901126.44)    "BEACON HILL"    "HILL"     "422135N0710400W"
    (236536.17, 901901.88)    "COPPS HILL"     "HILL"     "422200N0710325W"

Create a custom table of placenames by reading data from a file. Then, geocode a specified placename by searching the specified table variable of placenames.

Create the custom table by reading data from a shapefile that contains tsunami events. The table stores the placenames in the Location variable and the shape objects in the Shape variable.

customNamesGT = readgeotable("tsunamis.shp",CoordinateSystemType="geographic");
head(customNamesGT)
             Shape             Year    Month    Day    Hour    Minute    Second    Val_Code           Validity            Cause_Code         Cause          Eq_Mag         Country                 Location            Max_Height    Iida_Mag    Intensity    Num_Deaths    Desc_Deaths
    _______________________    ____    _____    ___    ____    ______    ______    ________    _______________________    __________    ________________    ______    _________________    ________________________    __________    ________    _________    __________    ___________

    ( 3.8000°S, 128.3000°E)    1950     10       8       3       23       NaN         2        "questionable tsunami"         1         "Earthquake"         7.6      "INDONESIA"          "JAVA TRENCH, INDONESIA"        2.8         1.5          1.5             0            0     
    (19.5000°N, 156.0000°W)    1951      8      21      10       57       NaN         4        "definite tsunami"             1         "Earthquake"         6.9      "USA"                "HAWAII"                        3.6         1.8          NaN             0            0     
    ( 9.0200°S, 157.9500°E)    1951     12      22       0        0       NaN         2        "questionable tsunami"         6         "Volcano"            NaN      "SOLOMON ISLANDS"    "KAVACHI"                         6         2.6          NaN             0            0     
    (42.1500°N, 143.8500°E)    1952      3       4       1       22        41         4        "definite tsunami"             1         "Earthquake"         8.1      "JAPAN"              "SE. HOKKAIDO ISLAND"           6.5         2.7            2            33            1     
    (19.1000°N, 155.0000°W)    1952      3      17       3       58       NaN         4        "definite tsunami"             1         "Earthquake"         4.5      "USA"                "HAWAII"                          1         NaN          NaN             0            0     
    (43.1000°N,  82.4000°W)    1952      5       6       0        0       NaN         1        "very doubtful tsunami"        9         "Meteorological"     NaN      "USA"                "LAKE HURON, MI"               1.52         NaN          NaN             0            0     
    (52.7500°N, 159.5000°E)    1952     11       4      16       58       NaN         4        "definite tsunami"             1         "Earthquake"           9      "RUSSIA"             "KAMCHATKA"                      18         4.2            4          2236            3     
    (50.0000°N, 156.5000°E)    1953      3      18       0        0       NaN         3        "probable tsunami"             1         "Earthquake"         5.8      "RUSSIA"             "N. KURIL ISLANDS"              1.5         0.6          NaN             0            0     

Geocode the placename Hawaii by searching the custom table. Specify the table variable containing the placenames as Location.

name = "Hawaii";
geocodedGT = geocode(name,customNamesGT,"Location");

View the output table. When you pass a custom table of placenames to the geocode function, the output table is a subset of the input table.

geocodedGT
geocodedGT=3×19 table
             Shape             Year    Month    Day    Hour    Minute    Second    Val_Code         Validity         Cause_Code              Cause               Eq_Mag    Country    Location    Max_Height    Iida_Mag    Intensity    Num_Deaths    Desc_Deaths
    _______________________    ____    _____    ___    ____    ______    ______    ________    __________________    __________    __________________________    ______    _______    ________    __________    ________    _________    __________    ___________

    (19.5000°N, 156.0000°W)    1951      8      21      10       57        NaN        4        "definite tsunami"        1         "Earthquake"                   6.9       "USA"     "HAWAII"        3.6         1.8          NaN           0              0     
    (19.1000°N, 155.0000°W)    1952      3      17       3       58        NaN        4        "definite tsunami"        1         "Earthquake"                   4.5       "USA"     "HAWAII"          1         NaN          NaN           0              0     
    (19.3340°N, 155.0240°W)    1975     11      29      14       47       40.4        4        "definite tsunami"        3         "Earthquake and Landslide"     7.1       "USA"     "HAWAII"       14.3           3            3           2              1     

Since R2025a

In many cases, the geocode function finds multiple matches for a specified placename. To return the first match, specify the FilterResults argument as "first".

Geocode the placename Essex using the county administrative level. Include only the first result in the output table.

geocodedGT = geocode("Essex","county",FilterResults="first")
geocodedGT=1×3 table
       Shape                Name             AdminLevel
    ____________    _____________________    __________

    geopolyshape    "Essex, England, GBR"     "county" 

Since R2025a

When you do not specify an administrative level, the geocode function searches the administrative levels in this order: "region", "state", "country", "city", "address", and "physical". By default, the function returns results for one administrative level, which is the first administrative level with matches. To return results for all the administrative levels, specify the FilterResults argument as "none".

Geocode the placename Pacific and return results for all the administrative levels.

geocodedGT = geocode("Pacific",FilterResults="none")
geocodedGT=9×3 table
        Shape                  Name               AdminLevel
    _____________    _________________________    __________

    geopolyshape     "Pacific"                    "region"  
    geopolyshape     "Pacific Ocean"              "region"  
    geopolyshape     "Pacific County, WA, USA"    "county"  
    geopointshape    "Pacific, MO, USA"           "city"    
    geopointshape    "Pacific, WA, USA"           "city"    
    geopointshape    "Pacific, WI, USA"           "city"    
    geopointshape    "Pacific, NC, USA"           "city"    
    geopointshape    "Pacific, CA, USA"           "city"    
    geopolyshape     "Pacific, BC, CAN"           "physical"

The output table contains matches for regions, counties, cities, and physical features. The function did not find matches for the state or address administrative levels.

Since R2025a

By default, the geocode function filters results from the online provider by applying partial string matching of leading characters. You can view unfiltered results from the online provider by specifying the PartialMatching name-value argument as false.

Geocode the placename Mount Fuji using the administrative level for physical features. By default, the function filters the results.

filteredGT = geocode("Mount Fuji","physical")
filteredGT=1×3 table
       Shape              Name           AdminLevel
    ____________    _________________    __________

    geopolyshape    "Mount Fuji, JPN"    "physical"

Geocode the placename again. This time, return the unfiltered results. The geospatial table contains additional results from the online provider.

unfilteredGT = geocode("Mount Fuji","physical",PartialMatch=false)
unfilteredGT=9×3 table
       Shape                           Name                        AdminLevel
    ____________    ___________________________________________    __________

    geopolyshape    "Mount Fuji, JPN"                              "physical"
    geopolyshape    "Mt. Fuji, Hidaka, Saitama, JPN"               "physical"
    geopolyshape    "Mt. Fuji, Minokamo, Gifu, JPN"                "physical"
    geopolyshape    "Mt. Fuji, Nasushiobara, Tochigi, JPN"         "physical"
    geopolyshape    "Mt. Fuji, Yokosuka, Kanagawa, JPN"            "physical"
    geopolyshape    "Mt. Fuji, Nishiaizu, Yama, Fukushima, JPN"    "physical"
    geopolyshape    "Mt. Fuji, Kasama, Ibaraki, JPN"               "physical"
    geopolyshape    "Mt. Fuji, Ueda, Nagano, JPN"                  "physical"
    geopolyshape    "Mt. Fuji, Gotemba, Shizuoka, JPN"             "physical"

Explore geocoded placenames on a map using custom data tips.

Geocode the placename Springfield using the city administrative level. Create a subtable of cities in the conterminous United States. The table stores the geocoded cities using point shapes in geographic coordinates.

cities = geocode("Springfield","city");

us = geocode("United States (conterminous)");
idx = isinterior(us.Shape,cities.Shape);
citiesInUS = cities(idx,:);

Display the cities on a map. Prepare to add data tips to the points by storing the Point object in pt.

figure
geobasemap grayland 
pt = geoplot(citiesInUS,MarkerSize=15);

Add the geocoded placenames to the data tip template. For more information about creating custom data tips, see Add Data Tips to Point, Line, and Polygon Shapes.

dtRow = dataTipTextRow("Name",citiesInUS.Name);
pt.DataTipTemplate.DataTipRows(end+1) = dtRow;

Create a data tip by using the datatip function, the Point object, and the geographic coordinates of a city. You can also add data tips by pausing on, clicking, or tapping the points.

dt = datatip(pt,44,-123);

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type point.

Input Arguments

collapse all

Placenames, specified as a string scalar, a vector of strings, a character vector, or a cell array of character vectors.

To use the online provider, you must specify this argument as a string scalar or a character vector. For more information about when the geocode function uses the online provider, see Algorithms.

Administrative level, specified as one of these options.

OptionDescriptionExamples
"region"World regions, including continents and oceans
  • Africa

  • Atlantic Ocean

"state"States and provinces
  • Massachusetts, USA

  • South Holland, NLD

"county" (since R2025a)Counties
  • Hertfordshire, England, GBR

  • San Bernardino County, CA, USA

"city"Cities
  • Beijing, CHN

  • Boston, MA, USA

"address" (since R2025a)Street addresses and postal codes
  • 1 Lakeside Campus Drive, Natick, MA, 01760, USA

  • 75007, Paris, Île-de-France, FRA

"physical" (since R2025a)Land and water features
  • Mount Fuji, JPN

  • Lake Superior, MI, USA

To return results for multiple administrative levels, specify the FilterResults name-value argument as "none" and do not specify a value for adminlevel.

The geocode function searches the administrative levels using the online provider and the installed list of placenames. For more information about the search algorithm for each administrative level, see Algorithms.

Custom placenames and shapes, specified as a geospatial table. For more information about geospatial tables, see Create Geospatial Tables.

customNamesGT must contain these table variables:

  • A Shape variable that specifies the point, line, or polygon shapes.

  • A variable that specifies the custom placenames. This variable must contain text data.

By default, the function searches for the specified placenames using the case-insensitive name table variable. If the table does not have a name variable, or has more than one name variable, then the function uses the second table variable. You can specify which table variable to search by using the namevar argument.

Table variable containing the custom placenames, specified using one of these indexing schemes. The table variable must contain text data.

Indexing SchemeExamples

Variable name:

  • A string scalar or character vector.

  • A pattern object. The pattern object must refer to only one variable.

  • "A" or 'A' — A variable named A

  • "Var"+digitsPattern(1) — The variable with the name "Var" followed by a single digit

Variable index:

  • An index number that refers to the location of a variable in the table.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double") — The variable containing double values

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: geocode(name,FilterResults="first") returns the first result.

Since R2025a

How to filter results, specified as "auto", "first", or "none". The behavior of FilterResults depends on whether you use the online provider and installed list of placenames or specify a custom table of placenames.

Online Provider and Installed ListCustom Table

Each option searches the administrative levels using the algorithm described in Algorithms.

  • "auto" — Stop searching when an administrative level contains results. Then, filter the results by applying the PartialMatch and IgnoreCase arguments. This option provides results from one administrative level.

  • "first" — Use the "auto" option, and then return the first result.

  • "none" — Search all of the administrative levels. Do not apply the PartialMatch or IgnoreCase arguments. This option can return results from multiple administrative levels.

Each option searches for the specified placename and filters the results by applying the PartialMatch and IgnoreCase arguments.

  • "auto" — If the function finds exactly one match, return the match. Otherwise, issue an error.

  • "first" — Return the first match.

  • "none" — Return all of the matches.

Option to use partial string matching, specified as a numeric or logical 1 (true) or 0 (false). The behavior of PartialMatch depends on whether the function finds results using the installed list, a custom table, or the online provider.

OptionInstalled List or Custom TableOnline Provider
true

Enable partial string matching of leading characters.

For example, geocode("Mass","state") returns a result for Massachusetts.

After obtaining results from the online provider, filter the results by applying partial string matching of leading characters.

For example, the result of geocode("Macon","city") includes matches for Macon, but not for Mâcon.

The function applies partial string matching when name contains only letters, spaces, and commas. The function does not apply partial string matching when the administrative level is "address".

false

Do not enable partial string matching. This option requires you to specify placenames that are exact matches for placenames in the installed list.

For example, geocode("Mass","state",PartialMatch=false) does not return a result from the installed list.

Do not use partial string matching to filter results from the online provider.

For example, the result of geocode("Macon","city",PartialMatch=false) includes matches for both Macon and Mâcon.

Option to ignore case, specified as a numeric or logical 1 (true) or 0 (false).

  • true — Ignore case.

  • false — Do not ignore case.

Output Arguments

collapse all

Geocoded placenames and shapes, returned as a geospatial table.

When you use the online provider and installed list of placenames, the geospatial table contains three variables:

  • Shape — Shape of the geocoded placename, returned as a geopolyshape or geopointshape object. The type of object depends on the administrative level of the geocoded placename.

    • When the administrative level is "region" or "state", the shape is a geopolyshape object that represents either an outline or a quadrangle that approximates the region. Quadrangles can cover a different area than the outline of the region.

    • When the administrative level is "county" or "physical", the shape is a geopolyshape object that represents a quadrangle that approximates the region. Quadrangles can cover a different area than the outline of the region.

    • When the administrative level is "city" or "address", the shape is a geopointshape object that represents the location of the city or address.

  • Name — Geocoded placename, returned as a string scalar.

  • AdminLevel — Administrative level, returned as "region", "state", "county", "city", "address", or "physical".

When you use the online provider and installed list of placenames, the geospatial table has a maximum of 50 rows.

When you use a custom table of placenames by specifying the customNamesGT argument, the output geospatial table is a subset of the custom table. As a result, the output geospatial table has the same table variables as the custom table.

Limitations

When the geocode function uses the online provider, deployment using MATLAB® Compiler™ is not supported. For information about when the function uses the online provider, see Algorithms.

More About

collapse all

Tips

  • The online provider updates the data periodically. As a result, the geocode function can return different results over time.

  • In offline environments:

    • Geocoding the names of regions, states, and cities is limited to the installed list of placenames. You can query the installed list by using the placenames function.

    • Geocoding the names of counties, addresses, and physical features is not supported.

Algorithms

collapse all

Version History

Introduced in R2024b

expand all