MATLAB: How to speed up “plotm” when plotting large amounts of map data in a loop

largeMapping Toolboxperformanceplotmscattermslow

I have a large amount of map data that I am plotting in a loop using "plotm". This loop becomes slower over time and takes a while to complete.
% numPoints = 200,000
axesm mercator
for i = 1:numPoints
plotm(lat(i), lon(i));
end
Is there some way to speed up this plotting?

Best Answer

The slowdown is due to the large number of graphics objects created by the script -- each call to "plotm" creates a separate graphics object which needs to be initialized and maintained in memory.
If you are plotting individual points with no connections between them, consider using the "scatterm" function instead.
scatterm(lat, lon);
If you are plotting groups of points (such as polygons) that should have connections between them, you can concatenate the data for each group into a single vector and separate the groups with NaN values:
% First polygon
lat1 = [0 10 10 0 0];
lon1 = [0 0 10 10 0];
% Second polygon
lat2 = lat1 + 20;
lon2 = lon1;
% Separate with NaN's then call "plotm" once
combinedLat = [lat1, NaN, lat2];
combinedLon = [lon1, NaN, lon2];
plotm(combinedLat, combinedLon);