


HGlogmenu Adds a context menu for toggling logarithmic scaling. HGlogmenu creates a context menu that allows the user to toggle logarithmic scaling for image, surface or line data, depending on the contents of the current axes. If the current axes contain an image, the context menu is associated with that object (or the topmost one if multiple images are present). Pixel intensities toggle between their values when HGlogmenu is called and the log (base 10) of those values (raw data values <= 0 result in NaNs). The log data is precomputed when the menu is first added to an image to speed up subsequent switching. If no image is present but the axes contain surface objects, the context menu is associated with the axes. The menu then toggles linear vs logarithmic scaling on the z-axis. If no image or surface objects are present but the axes contain line objects, the context menu is associated with the axes. In this case, the menu toggles logarithmic scaling on the y-axis. HGlogmenu(HANDLE) associates the menu with the object specified by HANDLE. H = HGlogmenu(...) returns a handle to the object (image or axes) associated with the new context menu.


0001 function h = HGlogmenu(h) 0002 %HGlogmenu Adds a context menu for toggling logarithmic scaling. 0003 % HGlogmenu creates a context menu that allows the user to toggle 0004 % logarithmic scaling for image, surface or line data, depending on the 0005 % contents of the current axes. 0006 % 0007 % If the current axes contain an image, the context menu is associated 0008 % with that object (or the topmost one if multiple images are present). 0009 % Pixel intensities toggle between their values when HGlogmenu is called 0010 % and the log (base 10) of those values (raw data values <= 0 result in 0011 % NaNs). The log data is precomputed when the menu is first added to an 0012 % image to speed up subsequent switching. 0013 % 0014 % If no image is present but the axes contain surface objects, the 0015 % context menu is associated with the axes. The menu then toggles 0016 % linear vs logarithmic scaling on the z-axis. 0017 % 0018 % If no image or surface objects are present but the axes contain line 0019 % objects, the context menu is associated with the axes. In this case, 0020 % the menu toggles logarithmic scaling on the y-axis. 0021 % 0022 % HGlogmenu(HANDLE) associates the menu with the object specified by 0023 % HANDLE. 0024 % 0025 % H = HGlogmenu(...) returns a handle to the object (image or axes) 0026 % associated with the new context menu. 0027 0028 state = warning; warning off; 0029 0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0031 if (nargin > 1) 0032 error('Invalid syntax.'); 0033 elseif (nargin == 1) 0034 switch(get(h,'Type')), 0035 case 'image', limits = 'CLim'; 0036 case 'surface', limits = 'ZLim'; 0037 case 'line', limits = 'YLim'; 0038 otherwise, 0039 error(sprintf('HGlogmenu is undefined for objects of type %s.', get(h,'Type'))); 0040 end 0041 else 0042 childs = get(gca, 'Children'); 0043 img = findobj(childs, 'Type', 'image'); 0044 if (~isempty(img)), 0045 h = img(1); limits = 'CLim'; 0046 elseif (~isempty(findobj(childs, 'Type', 'surface'))), 0047 h = gca; limits = 'ZLim'; 0048 elseif (~isempty(findobj(childs, 'Type', 'line'))), 0049 h = gca; limits = 'YLim'; 0050 else 0051 error('The current axes do not contain the appropriate objects.'); 0052 end 0053 end 0054 0055 %%%%%%%%%%%%%%%%%%%%%%%%% Define the Callback %%%%%%%%%%%%%%%%%%%%%%%% 0056 cmenu = get(h, 'UIContextMenu'); 0057 if (isempty(cmenu)), cmenu = uicontextmenu; set(h, 'UIContextMenu', cmenu); end; 0058 logmenu = findobj(cmenu, 'Tag', 'logmenu'); 0059 if (isempty(logmenu)), % if menu doesn't exist yet ... 0060 logmenu = uimenu(cmenu, 'Tag', 'logmenu', 'Checked', 'off', ... 0061 'Label', 'Log scaling', 'Callback', @CB_logmenu); 0062 end 0063 0064 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Store UserData %%%%%%%%%%%%%%%%%%%%%%%%%% 0065 switch(limits) 0066 case {'YLim','ZLim'} 0067 userdata.axes = h; 0068 case 'CLim', 0069 userdata.axes = get(h, 'Parent'); 0070 userdata.imageobj = h; 0071 userdata.backdata = log10(get(h, 'CData')); 0072 userdata.backdata(get(h,'CData') <= 0) = NaN; % blank out imaginary/-INF values 0073 userdata.backlims = [min(userdata.backdata(:)), max(userdata.backdata(:))]; % default tight color scaling on log plot 0074 end 0075 userdata.limits = limits; 0076 set(logmenu, 'UserData', userdata); 0077 0078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Cleanup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0079 if (nargout == 0), clear h; end 0080 0081 warning(state);