Main Content

Interactively Display Text on Maps

Display text on maps by interactively selecting the locations for the text.

This page shows how to create similar maps using map axes (since R2023a) and axesm-based maps. For a comparison of map axes and axesm-based maps, including when to use each type of display, see Choose a 2-D Map Display.

Load Data

Read a shapefile containing polygon shapes for each of the US states into a geospatial table.

states = readgeotable("usastatehi.shp");

Display Text on Map Axes

Interactively display text on map axes by using the gtext function. To display text on map axes by specifying numeric arguments, use the text function.

Set up a map using a projected coordinate reference system (CRS) that is appropriate for the conterminous United States. Create the projected CRS using the ESRI code 102003.

figure
proj = projcrs(102003,Authority="ESRI");
newmap(proj)

geoplot(states)
hold on

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

Label Maine, Florida, and Texas by displaying text on the map. To use predefined locations, specify interactivelySelectLocations as false. To interactively select the locations, specify interactivelySelectLocations as true.

txt = ["Maine"; "Florida"; "Texas"];
interactivelySelectLocations = false;
if interactivelySelectLocations
    gtext(txt)
else
    lat = [47.5329 27.4865 27.2904];
    lon = [-76.9502 -88.3942 -105.6624]; 
    text(lat,lon,txt)
end

Figure contains an axes object with type mapaxes. The mapaxes object contains 4 objects of type polygon, text.

If you interactively select a location outside of the map outline, then the gtext function might not display the text or might display the text in an unexpected location.

Display Text on axesm-Based Maps

Interactively identify locations on axesm-based maps by using the gtextm function. To display text on axesm-based maps by specifying numeric arguments, use the textm function.

Create an axesm-based map that is appropriate for the conterminous United States. Provide geographic context for the map by displaying the land areas.

figure
usamap conus
geoshow(states)

Figure contains an axes object. The hidden axes object contains 61 objects of type patch, line, text.

Label Maine, Florida, and Texas by displaying text on the map. To use predefined locations, specify interactivelySelectLocations as false. To interactively select the locations, specify interactivelySelectLocations as true.

txt = ["Maine"; "Florida"; "Texas"];
interactivelySelectLocations = false;
if interactivelySelectLocations
    gtextm(txt)
else
    lat = [47.5329 27.4865 27.2904];
    lon = [-76.9502 -88.3942 -105.6624];  
    textm(lat,lon,txt)
end

Figure contains an axes object. The hidden axes object contains 64 objects of type patch, line, text.

If you interactively select a location outside of the map frame, then the gtextm function might not display the text or might display the text in an unexpected location.

See Also

Functions

Related Topics