image thumbnail

Axis context menu for toggling X/Y scale and limits

by

 

Adds a special context menu to the axis that helps toggling the scale and limits of the axes.

toggleAxisContextMenu(ax)
function toggleAxisContextMenu(ax)
   %Adds scale (linear / log) and axis limit (auto / manual) toggling  as a contextmenu of the axis.
   %toggleAxisContextMenu()
   %toggleAxisContextMenu(hAxes)
   %
   %example: 
   %toggleAxisContextMenu(gca);
   %
   %Important: This function relies on the uiaxiscontextmenu by Lior Cohen.
   %It can be downloaded from http://bit.ly/WLTS9E
   %Please make sure it is in the Matlab path or in your working directory
   %
   %By Lior Cohen Jan 2013
   %see also uiaxiscontextmenu
   
    if nargin==0
        ax = gca;
    end
    %
    try
        uiaxiscontextmenu(ax);
    catch %#ok<CTCH>
        err = lasterror; %#ok<LERR> %to support <2010a version
        if isempty( which('uiaxiscontextmenu') )
            error('LC:FileIsMissing',...
                'uiaxiscontextmenu is missing. Please download it from <a href="http://bit.ly/WLTS9E">http://bit.ly/WLTS9E</a> and add to  path');
        end   
        rethrow(err);
    end
    
    cm = uicontextmenu;
    uimenu(cm, 'label', 'Log Scale', 'callback', @(h,e)toggleScale(h,ax,'Y') );
    uimenu(cm, 'label', 'Lock', 'callback', @(h,e)toggleLimMode(h,ax,'Y') );
    set(ax,'YUIContextMenu', cm);
    
    cm = uicontextmenu;
    uimenu(cm, 'label', 'Log Scale', 'callback', @(h,e)toggleScale(h,ax,'X') );
    uimenu(cm, 'label', 'Lock', 'callback', @(h,e)toggleLimMode(h, ax,'X') );
    set(ax,'XUIContextMenu', cm);
end

function toggleScale(h, ax, axis)
    scale = get(ax, [axis, 'Scale'] );
    if strcmp(scale, 'linear')
        set(ax, [axis, 'Scale'], 'log' );
        set(h,'label', 'Linear Scale')
    else
        set(ax, [axis, 'Scale'], 'linear' );
        set(h,'label', 'Log Scale')
    end
end


function toggleLimMode(h, ax, axis)
    scale = get(ax, [axis, 'LimMode'] );
    if strcmp(scale, 'auto')
        set(ax, [axis, 'LimMode'], 'manual' );
        set(h,'label', 'UnLock')
    else
        set(ax, [axis, 'LimMode'], 'auto' );
        set(h,'label', 'Lock')
    end
end

Contact us