Home > FMAToolbox > Plot > Browse.m

Browse

PURPOSE ^

Browse - Interactively browse the data in one or all subplots in a figure.

SYNOPSIS ^

function Browse(parameter1,parameter2)

DESCRIPTION ^

Browse - Interactively browse the data in one or all subplots in a figure.

  USAGE

    Browse(figure,'on')   % enable browsing for this figure
    Browse(figure,'off')  % disable browsing for this figure
    Browse('on')          % enable browsing for all new figures
    Browse('off')         % disable browsing for new figures
    Browse('all')         % enable browsing for all existing figures
    Browse('none')        % disable browsing for all existing figures

    Browse(figure)        % same as Browse(figure,'on')
    Browse                % same as Browse(gcf,'on')

  NOTE

    It is generally a good idea to append "Browse('on');" in the <a href="matlab:edit startup.m">startup</a> file,
    so that it is automatically enabled for all figures as soon as Matlab is
    started.

  INTERACTIVE COMMANDS

    To browse the data, press a key (hold SHIFT to browse all subplots):

     left arrow     move one step to the left
     right arrow    move one step to the right
     page up        move one page to the left
     page down      move one page to the right
     home           move to the beginning
     end            move to the end
     i              zoom in (halve x axis width)
     o              zoom out (double x axis width)

    To graphically define new axis limits, click and drag the region of interest,
    then press a key to update the axes (hold SHIFT to update all subplots):

     x              change X axis
     y              change Y axis
     b              change both axes

    To reset the axis limits, press a key (hold SHIFT to reset all subplots):

     CTRL+x         reset X axis
     CTRL+y         reset Y axis
     CTRL+b         reset both axes

    To use a special interactive commmand, click and drag the region of interest,
    then press a key:

       m            show mean and std of data in selected area (image only)
       s            show X and Y coordinates of selected area

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function Browse(parameter1,parameter2)
0002 
0003 %Browse - Interactively browse the data in one or all subplots in a figure.
0004 %
0005 %  USAGE
0006 %
0007 %    Browse(figure,'on')   % enable browsing for this figure
0008 %    Browse(figure,'off')  % disable browsing for this figure
0009 %    Browse('on')          % enable browsing for all new figures
0010 %    Browse('off')         % disable browsing for new figures
0011 %    Browse('all')         % enable browsing for all existing figures
0012 %    Browse('none')        % disable browsing for all existing figures
0013 %
0014 %    Browse(figure)        % same as Browse(figure,'on')
0015 %    Browse                % same as Browse(gcf,'on')
0016 %
0017 %  NOTE
0018 %
0019 %    It is generally a good idea to append "Browse('on');" in the <a href="matlab:edit startup.m">startup</a> file,
0020 %    so that it is automatically enabled for all figures as soon as Matlab is
0021 %    started.
0022 %
0023 %  INTERACTIVE COMMANDS
0024 %
0025 %    To browse the data, press a key (hold SHIFT to browse all subplots):
0026 %
0027 %     left arrow     move one step to the left
0028 %     right arrow    move one step to the right
0029 %     page up        move one page to the left
0030 %     page down      move one page to the right
0031 %     home           move to the beginning
0032 %     end            move to the end
0033 %     i              zoom in (halve x axis width)
0034 %     o              zoom out (double x axis width)
0035 %
0036 %    To graphically define new axis limits, click and drag the region of interest,
0037 %    then press a key to update the axes (hold SHIFT to update all subplots):
0038 %
0039 %     x              change X axis
0040 %     y              change Y axis
0041 %     b              change both axes
0042 %
0043 %    To reset the axis limits, press a key (hold SHIFT to reset all subplots):
0044 %
0045 %     CTRL+x         reset X axis
0046 %     CTRL+y         reset Y axis
0047 %     CTRL+b         reset both axes
0048 %
0049 %    To use a special interactive commmand, click and drag the region of interest,
0050 %    then press a key:
0051 %
0052 %       m            show mean and std of data in selected area (image only)
0053 %       s            show X and Y coordinates of selected area
0054 
0055 % Copyright (C) 2004-2012 by Michaƫl Zugaro
0056 %
0057 % This program is free software; you can redistribute it and/or modify
0058 % it under the terms of the GNU General Public License as published by
0059 % the Free Software Foundation; either version 3 of the License, or
0060 % (at your option) any later version.
0061 
0062 % Name of this function
0063 name = mfilename;
0064 % Current default figure creation function
0065 default = get(0,'DefaultFigureCreateFcn');
0066 if isa(default,'function_handle'), default = func2str(default); end
0067 
0068 % Special cases: Browse('on') and Browse('off')
0069 if nargin == 1 && isastring(lower(parameter1),'on','off','all','none'),
0070     % Current default figure creation function
0071     switch lower(parameter1),
0072         case 'on',
0073             if isempty(regexp(default,name)),
0074                 set(0,'DefaultFigureCreateFcn',[name ';' default]);
0075             end
0076             help
0077         case 'off',
0078             if ~isempty(regexp(default,name)),
0079                 set(0,'DefaultFigureCreateFcn',regexprep(default,[name ';'],''));
0080             end
0081         case 'all',
0082             children = get(0,'children');
0083             for i = 1:length(children),
0084                 set(children(i),'WindowButtonDownFcn',@DefineSelection);
0085                 set(children(i),'KeyPressFcn',@KeyPress);
0086             end
0087         case 'none',
0088             children = get(0,'children');
0089             for i = 1:length(children),
0090                 set(children(i),'WindowButtonDownFcn',[]);
0091                 set(children(i),'KeyPressFcn',[]);
0092             end
0093     end
0094     return
0095 end
0096 
0097 % Special case: call upon figure creation
0098 if ~isempty(gcbo),
0099     % Current figure creation function
0100     current = get(gcbo,'CreateFcn');
0101     if isa(current,'function_handle'), current = func2str(current); end
0102     set(gcbo,'CreateFcn',regexprep(current,[name ';'],''));
0103 end
0104 
0105 % General case: parameter1 is a figure handle
0106 if nargin == 0,
0107     fig = gcf;
0108 else
0109     fig = parameter1;
0110 end
0111 
0112 if nargin == 2 && strcmp(lower(parameter2),'off')
0113     set(fig,'WindowButtonDownFcn',[]);
0114     set(fig,'KeyPressFcn',[]);
0115 else
0116     set(fig,'WindowButtonDownFcn',@DefineSelection);
0117     set(fig,'KeyPressFcn',@KeyPress);
0118 end
0119 
0120 %-----------------------------------------------------------------------------------------------------------------
0121 %   Help message
0122 %-----------------------------------------------------------------------------------------------------------------
0123 
0124 function help
0125 
0126 disp(' ------------------------------------------------------------------------------');
0127 disp('  Browsing mode automatically enabled for new figures.');
0128 disp('  ');
0129 disp('  To browse current axes, press a key (hold SHIFT to browse all subplots):');
0130 disp('  ');
0131 disp('     left arrow     move one step to the left');
0132 disp('     right arrow    move one step to the right');
0133 disp('     page up        move one page to the left');
0134 disp('     page down      move one page to the right');
0135 disp('     home           move to the beginning');
0136 disp('     end            move to the end');
0137 disp('     i              zoom in (halve x axis width)');
0138 disp('     o              zoom out (double x axis width)');
0139 disp('  ');
0140 disp('  To graphically define new axis limits, click and drag the region of interest,');
0141 disp('  then press a key to update the axes (hold SHIFT to update all subplots):');
0142 disp('  ');
0143 disp('     x              change X axis');
0144 disp('     y              change Y axis');
0145 disp('     b              change both axes');
0146 disp('  ');
0147 disp('  To reset the axis limits, press a key (hold SHIFT to reset all subplots):');
0148 disp('  ');
0149 disp('     CTRL+x         reset X axis');
0150 disp('     CTRL+y         reset Y axis');
0151 disp('     CTRL+b         reset both axes');
0152 disp('  ');
0153 disp('  To copy/paste axis limits, press a key:');
0154 disp('  ');
0155 disp('     c              copy current axis limits');
0156 disp('     v              apply (paste) previously copied limits to current axis');
0157 disp('  ');
0158 disp('  To use a special interactive commmand, click and drag the region of interest,');
0159 disp('  then press a key:');
0160 disp('  ');
0161 disp('     m               show mean and std over selected area (image only)');
0162 disp('     s               show coordinates of selected area');
0163 disp(' ------------------------------------------------------------------------------');
0164 
0165 %-----------------------------------------------------------------------------------------------------------------
0166 %   Mouse click and drag
0167 %-----------------------------------------------------------------------------------------------------------------
0168 
0169 function DefineSelection(fig,event)
0170 
0171 if isempty(get(fig,'CurrentAxes')), return; end
0172 
0173 % Let user select rectangle and store info until a key is pressed
0174 point1 = get(gca,'CurrentPoint');
0175 rbbox;
0176 point2 = get(gca,'CurrentPoint');
0177 x1 = point1(1,1);
0178 x2 = point2(1,1);
0179 xLim = [min([x1 x2]) max([x1 x2])];
0180 y1 = point1(1,2);
0181 y2 = point2(1,2);
0182 yLim = [min([y1 y2]) max([y1 y2])];
0183 set(fig,'UserData',[xLim;yLim]);
0184 
0185 %-----------------------------------------------------------------------------------------------------------------
0186 %   Key press
0187 %-----------------------------------------------------------------------------------------------------------------
0188 
0189 function KeyPress(fig,event)
0190 
0191 if isempty(get(fig,'CurrentAxes')), return; end
0192 
0193 if event.Key == 's',
0194     DisplaySelection(fig,event);
0195 elseif event.Key == 'm',
0196     PlotSelectionMean(fig,event);
0197 elseif event.Key == 'c',
0198     CopyAxisLimits(fig,event);
0199 elseif event.Key == 'v',
0200     PasteAxisLimits(fig,event);
0201 else
0202     ChangeLims(fig,event);
0203 end
0204 
0205 %-----------------------------------------------------------------------------------------------------------------
0206 %   Display selection X and Y coordinates
0207 %-----------------------------------------------------------------------------------------------------------------
0208 
0209 function DisplaySelection(fig,event)
0210 
0211 if isempty(get(fig,'CurrentAxes')), return; end
0212 
0213 % Retrieve limits, based on previous mouse selection
0214 lims = get(fig,'UserData');
0215 if isempty(lims), return; end
0216 xLim = lims(1,:);
0217 yLim = lims(2,:);
0218 
0219 x1 = num2str(xLim(1));
0220 y1 = num2str(yLim(1));
0221 n = length(x1)-length(y1);
0222 spaces = repmat(' ',1,abs(n));
0223 if n < 0, x1 = [spaces x1]; else y1 = [spaces y1] ; end
0224 x2 = num2str(xLim(2));
0225 y2 = num2str(yLim(2));
0226 n = length(x2)-length(y2);
0227 spaces = repmat(' ',1,abs(n));
0228 if n < 0, x2 = [spaces x2]; else y2 = [spaces y2] ; end
0229 
0230 disp(['X = [' x1 '   ' x2 ']   dX = ' num2str(diff(xLim))]);
0231 disp(['Y = [' y1 '   ' y2 ']   dY = ' num2str(diff(yLim))]);
0232 
0233 %-----------------------------------------------------------------------------------------------------------------
0234 %   Plot mean and std of data enclosed in selection
0235 %-----------------------------------------------------------------------------------------------------------------
0236 
0237 function PlotSelectionMean(fig,event)
0238 
0239 if isempty(get(fig,'CurrentAxes')), return; end
0240 
0241 % Find image in gca (if any)
0242 children = get(gca,'children');
0243 img = [];
0244 for i = 1:length(children),
0245     if strcmp(get(children(i),'type'),'image'),
0246         img = children(i);
0247     end
0248 end
0249 if isempty(img), return; end
0250 
0251 % Retrieve limits, based on previous mouse selection
0252 lims = get(fig,'UserData');
0253 if isempty(lims), return; end
0254 xLim = lims(1,:);
0255 yLim = lims(2,:);
0256 % Compute and plot mean
0257 data = get(img,'cdata');
0258 x = get(img,'xdata');
0259 x = linspace(x(1),x(end),size(data,2));
0260 y = get(img,'ydata');
0261 y = linspace(y(1),y(end),size(data,1));
0262 xOK = x >= xLim(1) & x <= xLim(2);
0263 yOK = y >= yLim(1) & y <= yLim(2);
0264 m = mean(data(yOK,xOK),2);
0265 s = std(data(yOK,xOK),0,2);
0266 figure;
0267 PlotMean(y(yOK),m,m-s,m+s,':');
0268 
0269 %-----------------------------------------------------------------------------------------------------------------
0270 %   Copy current axis limits
0271 %-----------------------------------------------------------------------------------------------------------------
0272 
0273 function CopyAxisLimits(fig,event)
0274 
0275 % (using weird names makes it unlikely that these globals could be used somewhere else)
0276 global xAxisLimits_Browse49 yAxisLimits_Browse49;
0277 
0278 xAxisLimits_Browse49 = xlim;
0279 yAxisLimits_Browse49 = ylim;
0280 disp(['Copy X axis [' num2str(xAxisLimits_Browse49(1)) '   ' num2str(xAxisLimits_Browse49(2)) ']']);
0281 disp(['Copy Y axis [' num2str(yAxisLimits_Browse49(1)) '   ' num2str(yAxisLimits_Browse49(2)) ']']);
0282 
0283 %-----------------------------------------------------------------------------------------------------------------
0284 %   Apply (paste) previously copied limits to current axis
0285 %-----------------------------------------------------------------------------------------------------------------
0286 
0287 function PasteAxisLimits(fig,event)
0288 
0289 % (using weird names makes it unlikely that these globals could be used somewhere else)
0290 global xAxisLimits_Browse49 yAxisLimits_Browse49;
0291 
0292 if ~isempty(xAxisLimits_Browse49) && ~isempty(yAxisLimits_Browse49),
0293     disp(['Paste X axis [' num2str(xAxisLimits_Browse49(1)) '   ' num2str(xAxisLimits_Browse49(2)) ']']);
0294     disp(['Paste Y axis [' num2str(yAxisLimits_Browse49(1)) '   ' num2str(yAxisLimits_Browse49(2)) ']']);
0295     xlim(xAxisLimits_Browse49);
0296     ylim(yAxisLimits_Browse49);
0297 end
0298 
0299 %-----------------------------------------------------------------------------------------------------------------
0300 %   Change axes limits
0301 %-----------------------------------------------------------------------------------------------------------------
0302 
0303 function ChangeLims(fig,event)
0304 
0305 if isempty(get(fig,'CurrentAxes')), return; end
0306 
0307 % Define a few constants
0308 NONE = 0;
0309 RIGHT_PAGE = 1;
0310 LEFT_PAGE = 2;
0311 RIGHT_STEP = 3;
0312 LEFT_STEP = 4;
0313 HOME = 5;
0314 END = 6;
0315 
0316 % Default behavior (if no modifiers are pressed): modify only current axes, based on previous mouse selection
0317 children = gca;
0318 auto = false;
0319 for i = 1:length(event.Modifier),
0320     if strcmp(event.Modifier{i},'shift'),
0321         % Shift pressed => change all subplots
0322         children = get(fig,'children');
0323     elseif strcmp(event.Modifier{i},'control'),
0324         % Control pressed => reset selected axes
0325         auto = true;
0326     end
0327 end
0328 
0329 % Which action?
0330 changeX = false;
0331 changeY = false;
0332 move = NONE;
0333 zoomX = 1;
0334 switch(event.Key),
0335     case 'x',
0336         changeX = true;
0337     case 'y',
0338         changeY = true;
0339     case 'b',
0340         changeX = true;
0341         changeY = true;
0342     case 'pagedown',
0343         move = RIGHT_PAGE;
0344     case 'pageup',
0345         move = LEFT_PAGE;
0346     case 'rightarrow',
0347         move = RIGHT_STEP;
0348     case 'leftarrow',
0349         move = LEFT_STEP;
0350     case 'home',
0351         move = HOME;
0352     case 'end',
0353         move = END;
0354     case 'i',
0355         zoomX = 0.5;
0356     case 'o',
0357         zoomX = 2;
0358     otherwise,
0359         return
0360 end
0361 
0362 % Update subplot(s)
0363 for i = 1:length(children),
0364     type = get(children(i),'type');
0365     if strcmp(type,'axes'),
0366         if move ~= NONE,
0367             % Move x lims
0368             x = xlim(children(i));
0369             dx = x(2)-x(1);
0370             switch(move),
0371                 case RIGHT_PAGE,
0372                     xLim = x+dx;
0373                 case LEFT_PAGE,
0374                     xLim = x-dx;
0375                 case RIGHT_STEP,
0376                     xLim = x+dx/10;
0377                 case LEFT_STEP,
0378                     xLim = x-dx/10;
0379                 case HOME,
0380                     % Determine min x value for all data
0381                     c = get(children(i),'children');
0382                     minX = Inf;
0383                     for j = 1:length(c),
0384                         m = get(c(j),'xdata');
0385                         minX = min([minX m(1)]);
0386                     end
0387                     xLim = minX + [0 dx];
0388                 case END,
0389                     % Determine max x value for all data
0390                     c = get(children(i),'children');
0391                     maxX = -Inf;
0392                     for j = 1:length(c),
0393                         m = get(c(j),'xdata');
0394                         maxX = max([maxX m(end)]);
0395                     end
0396                     xLim = maxX - [dx 0];
0397             end
0398             set(children(i),'xlim',xLim);
0399             % Reset mouse selection area
0400             set(fig,'UserData',[]);
0401         elseif zoomX ~= 1,
0402             % Zoom x axis
0403             xLim = get(children(i),'xlim');
0404             set(children(i),'xlim',[xLim(1) xLim(1)+zoomX*(xLim(2)-xLim(1))]);
0405         else
0406             % Retrieve limits, based on previous mouse selection
0407             lims = get(fig,'UserData');
0408             if ~auto && isempty(lims), return; end
0409             if ~isempty(lims),
0410                 xLim = lims(1,:);
0411                 yLim = lims(2,:);
0412             end
0413             % Change x and/or y lims
0414             if changeX,
0415                 if auto,
0416                     set(children(i),'xlimmode','auto');
0417                 else
0418                     if xLim(1) ~= xLim(2), set(children(i),'xlim',xLim); end
0419                 end
0420             end
0421             if changeY,
0422                 if auto,
0423                     set(children(i),'ylimmode','auto');
0424                 else
0425                     if yLim(1) ~= yLim(2), set(children(i),'ylim',yLim); end
0426                 end
0427             end
0428         end
0429     end
0430 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005