Search code examples
matlabmappingaxesmultiple-axes

Switch between two map axes in Matlab


Is there a way to switch between two map axes in the mapping toolbox in Matlab?

I created a basemap, then I created a small inset to show the map location in a more zoomed-out context.

I'd like to go back and add to the first map, but the inset is now active, so if I then use plotm(lat, lon) it appears on the small inset.

I know I can just change the order of plotting and do the inset last, but at this point I haven't decided how I want the final map to look so at this "exploration phase" I'd like to be able to toggle between the two map axes on the fly, if it is possible. Ideally I'd like to make the basemap a function that I can call repeatedly and add to.

Simplified example code:

figure(10);
% create main map
ax1 = axesm('mercator', 'MapLatLim', [33.12 33.48], 'MapLonLim', [-118.9 -118.3], 'Frame', 'on');
states = shaperead('usastatehi', 'UseGeoCoords', true, 'BoundingBox', [-118.9 33.12; -118.3 33.48]);
geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')

% create inset
gInset = axes('position', [0.60 0.18 .18 .14], 'Visible', 'off');
ax2 = axesm('mercator', 'MapLatLim', [32.2 34.8], 'MapLonLim', [-121.5 -116.5], ...
    'Frame', 'on', 'FLineWidth',1);
geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')

% add something to main map
plotm([33.2 33.45], [-118.5 -118.6])

Solution

  • Cecilia had the answer, but here is some more detail, if you need it.

    You can use the axes function to set the current active axes and switch between them. To do this in your code:

    figure(10);
    % create main map
    ax1 = axesm('mercator', 'MapLatLim', [33.12 33.48], 'MapLonLim', [-118.9 -118.3], 'Frame', 'on');
    states = shaperead('usastatehi', 'UseGeoCoords', true, 'BoundingBox', [-118.9 33.12; -118.3 33.48]);
    geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
    
    % create inset
    gInset = axes('position', [0.60 0.18 .18 .14], 'Visible', 'off');
    ax2 = axesm('mercator', 'MapLatLim', [32.2 34.8], 'MapLonLim', [-121.5 -116.5], ...
        'Frame', 'on', 'FLineWidth',1);
    geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
    
    % add something to main map
    plotm([33.2 33.45], [-118.5 -118.6])
    figure(10);
    % create main map
    ax1 = axesm('mercator', 'MapLatLim', [33.12 33.48], 'MapLonLim', [-118.9 -118.3], 'Frame', 'on');
    states = shaperead('usastatehi', 'UseGeoCoords', true, 'BoundingBox', [-118.9 33.12; -118.3 33.48]);
    geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
    
    % create inset
    gInset = axes('position', [0.60 0.18 .18 .14], 'Visible', 'off');
    ax2 = axesm('mercator', 'MapLatLim', [32.2 34.8], 'MapLonLim', [-121.5 -116.5], ...
        'Frame', 'on', 'FLineWidth',1);
    geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
    
    % add something to main map
    axes(ax1)
    plotm([33.2 33.45], [-118.5 -118.6])
    
    % to go back to ax2 and add to that again
    axes(ax2)