* * * The functions on this page are no longer being updated. They should still work as shown in the examples here, but I am only actively maintaining the versions of these functions which are in the Climate Data Toolbox for MATLAB, which can be found here https://www.mathworks.com/matlabcentral/fileexchange/70338. * * *
This submission contains functions to plot the outlines and names of National borders and US states. Matlab's Mapping Toolbox is NOT required.
There are two functions for plotting: borders and bordersm, and they both work the same way, except that bordersm is for use with maps created using Matlab's Mapping Toolbox. Similarly, labelborders and labelbordersm place text labels within the boundaries of countries or states.
Greene, Chad A., et al. “The Climate Data Toolbox for MATLAB.” Geochemistry, Geophysics, Geosystems, American Geophysical Union (AGU), July 2019, doi:10.1029/2019gc008392.
Inspired by: WORLDDATAMAP, Google Earth Toolbox, zoharby/plot_google_map, land_or_ocean.m, landmask, strlookup
Inspired: EOF, patchsc, scattertextm, Automated importation of sea surface temperature data, Arctic Sea Ice
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
I will say though, missing "Republic of South Sudan / South Sudan / Southern Sudan" (https://en.wikipedia.org/wiki/South_Sudan) which became recognized by a myriad of countries in 2011.
Amazing job. I'm not sure how or why, but whenever I'm searching for Matlab answers, Chad Greene is the one that saves the day.
Thanks for this package! It would be awesome if you some missing European countries like North Macedonia and Moldova could be included, making it more useful for European mapping.
@A: Good question! The easiest solution I can think of is to create an extra set of axes, something like this:
figure
borders('continental us')
axis off
pos = get(gca,'pos');
axes('position',[pos(1) pos(2) 0.2 0.2])
borders('alaska')
axis([-180 -129 51 72])
axis off
Thanks for this file. It's really helpful, and easy to use. Is there a way to tighten up the map, so that Alaska and Hawaii aren't so far away? It ends up distorting the map when they are added.
found Loas - was my fault for missing Lao People's Democratic Republic in the (extrermly) useful did you mean list that it outputs when it can find the name.
cant edit my previous comment so just adding another one.
Excellent work Chad!
This is a great function and easy to use. The examples are very clear. Easy to install
For some reason Laos is not included in the list of countries and the only one you can colour in. I've looked at borderdata.mat and there's nothing I can see - might be an issue outside this toolbox though.
Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Taiwan belongs to China!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Very nice function! How to use this function inside UI axes?
@Abdul: The borderdata.mat file is included in the zip file.
Where can I found the borderdata.mat?
Thank you so much chad ! Works perfectly now :)
Hmm, sorry about that Jacques. Maybe try deleting this block:
if license('test','map_toolbox')
assert(ismap(gca)==0,'...')
end
Hi, whether I use borders or bordersm I always get the same error :
"To use 'ismap', the following product must be both licensed and installed:
Mapping Toolbox"
I don't understand why.. Any help please ??
Thanks
Jacques
@Chad Small: Oh yes, I see the issue. This standalone version of the function is old, but you found the documentation for a newer version which is in the Climate Data Toolbox. I'll try to update this standalone version soon, but in the meantime you can get CDT here https://www.mathworks.com/matlabcentral/fileexchange/70338 and that should work.
Hi Chad,
This is an excellent tool. My only issue is that when I try to run the code to change my central longitude:
borders('countries','center',100)
It gives me back this error:
Error using plot
There is no center property on the Line class.
Error in borders (line 161)
h = plot(cell2nancat(lon),cell2nancat(lat),varargin{:});
Do you have any thoughts on why this might be, or is there another way to change my central longitude?
Thanks!
Very nice tool!
Excellent. And extremely easy to edit. 6 stars. Nothing more to say.
Looks like Moldova is missing
Good day. I have a problem with using this script, so my code:
figure
borders('Japan','color','k','linewidth',2);
axis tight
I have an error:
Error using matlab.graphics.axis.Axes/set
Children may only be set to a permutation of
itself
Error in
map.graphics.internal.restackMapAxes>moveToTop
(line 85)
set(ax,'Children',[h(:); c])
Error in map.graphics.internal.restackMapAxes
(line 72)
moveToTop(ax, hUserText)
Error in linem (line 115)
map.graphics.internal.restackMapAxes(h0)
Error in plotm (line 61)
hndl0 = linem(lat,lon,varargin{:});
Error in borders (line 197)
h = plotm(lat,lon,varargin{:});
nice
Thanks for your effort.
Thank you for the effort to make this excellent work.
Perhaps you might include continent border option such as 'europe' or 'asia'.
Would be great if you could also include the Chinese provinces, analogous to the US states. Note that
https://nl.mathworks.com/matlabcentral/fileexchange/29462-china-province-mat
is not fully compatible as the country borders of China are slightly different.
Hi Chad, I realy like your function but could you please add a patch for Europe.
@eko: Make sure you use the *hold on* command to make sure the pcolor information isn't lost. If the issue is that borders cover up your pcolor plot, call borders first. Or do something like
h = pcolor(lon,lat,z);
shading flat
hold on
borders
uistack(h,'top') % brings pcolor to the top
hello chad..
any idea how plot pcolor (colored) above borders..
in my case borders lost my pcolor information
tks
masterpiece code,thanks a lot!
Hi Sébastien,
I think you want to specify 'EdgeColor' instead of 'Color' if you're plotting as patches. That would look like this:
borders('countries', 'edgeColor', [1 0 0],'facecolor', [0 1 0])
Dear Chad,
You did a really interesting work and I'm using it regularly.
I'm not able to draw the all the country borders in one color and fill them with another color.
I'm trying to do Something like this:
borders('countries', 'Color', [1 0 0],'facecolor', [0 1 0])
but it does not work.
Could you help me please?
Best
Sébastien
Hi Dillon,
Good question! This solution's a little bit clunky, but it works. The idea is plot the borders twice, and move the second set over by 360 degrees longitude. Then set the axis limits however you'd like.
h1 = borders('countries','nomap');
h2 = borders('countries','nomap');
for k = 1:length(h2)
h2(k).XData = h2(k).XData + 360;
end
axis([0 360 -90 90])
Great function, Chad! Is there a way to make borders('countries','nomap') centered on the Pacific, though?
Hi Chad, This is such a great function!
Thanks for taking the time to provide it.
Can you tell me, do you know if it's possible to add countries to the list that are not included? Like Moldova or Macedonia?
Many thanks in advance:)
Hi Chad,
After I download this file, where should I put the files?
Thanks
Very simple to use! Here's a quick example:
borders('countries', 'Color', 'black')
hold on
borders('Brazil', 'FaceColor', 'green');
Hi,
Thank you for sharing.
How could I plot the boundary of the entire Europe?
Thanks,
Dong
Hi Casper,
I must have missed WobRotsons question. I will try to answer it and your question here. If you're using Matlab's Mapping Toolbox, set the axis limits with worldmap before plotting borders. Like this:
worldmap([-6 2],[-82 -74])
borders
Then add the lat,lon locations of platforms by
plotm(lat,lon,'o')
If you are not using Matlab's Mapping Toolbox, you can plot lons and lats just like they are x and y values. That would look like this:
axis([-82 -74 -6 2])
borders
plot(lon,lat,'bo')
If you're not using Matlab's Mapping Toolbox, you can always plot in "unprojected" coordinates, where you just treat longitudes like x values and latitudes like y values. If you don't mind some spatial distortion, that's probably okay.
Another option for your particular case is to use my arcticborders function, which plots in polar stereographic coordinates. Get it here: https://www.mathworks.com/matlabcentral/fileexchange/56923. Then to plot your platform locations use polarstereo_fwd (also on File Exchange) to transform your lat,lon locations to projected x,y coordinates, something like
[x,y] = polarstereo_fwd(lat,lon);
plot(x,y,'bo')
Hi Chad,
I am using your function for my school project on the North Sea, my first question is kind of the same as WobRotsen. Is it possible to plot a certain lat-lon box. My second question is of it is possible to plot certain coordinates (location of offshore platforms) in the map?
Thanks in advance!
is there a way to only plot within a certain lat-lon box? trying to plot up a map of Ecuador-Colombia-Peru borders within a -82:-74 lon -6:2 lat box. tried extracting the lat-lon data for each border as per doc file and then slecting only the relevant points but it leaves lines joining points from corners...
Hi Marina,
For a list of countries type
load borderdata.mat
sort(places)
Latin America is not one of the options, so you'll have to plot each country in Latin America individually. Alternatively, you could add this to the input parsing section:
if strncmpi(place,'latin america',4)
plotWhat = 'latin america';
end
and add this under "switch plotWhat"
case 'latin america'
lat = bd.lat([8 17 21 33 38 39 41 48 49 55 75 78 79 120 159 165 162 211 214]);
lon = bd.lon([8 17 21 33 38 39 41 48 49 55 75 78 79 120 159 165 162 211 214]);
although I can't guarantee the results. Good luck!
Is there a list with countries included in the borders pack?
I'd love to map Latin America, but it doesn't seem to be included...Thanks
Thanks a lot for these functions. It saves a lot of time for me!!!!
Is there anyway to put in a lat/lon area of interest? I'm trying to overlay borders on some gtopo30 data.
Jinny, It's not a wonderful solution but this works:
bd = load('borderdata.mat');
plot(bd.lon{209}(2684:9140), bd.lat{209}(2684:9140))
Thanks for this! Any way to plot just the continental US without the state borders?
Behnam, thanks for the note. The border outlines here are from the US Census Bureau 500k dataset. If you need higher resolution you might have to search the internet for France-specific outlines.
Thanks a lot for the input. The borders for France seems to have a bit mismatch in north-east parts. Could you please check it again?