Home > functions > gui > select_xrange.m

select_xrange

PURPOSE ^

select_xrange( key, varargin )

SYNOPSIS ^

function varargout = select_xrange(key, varargin)

DESCRIPTION ^

 select_xrange( key, varargin )

  2次元グラフのx軸の範囲をマウスドラッグで指定するための
  コールバックルーチンです

 key :  'init'  'delete'  'get'  'clear'  'SetBoxColor'
        'SetReturnVariable'  'SetButtonUpFcn'  'SetInitialRange'

   select_xrange( 'init', <axes handle>
       [ , 'BoxColor', <color> ]
       [ , 'ReturnVariable', <variable name> ]
       [ , 'ButtonUpFcn', <function string> ]
       [ , 'InitialRange', <range> ] )
     <axes handle> で指定されるグラフを選択対象に設定します。オプ
     ションとして、'BoxColor', 'BoxStyle',
     'ReturnVariable', 'ButtonUpFcn', 'InitialRange' を設定できま
     す。それぞれのオプションの詳細は下記を参照してください。

   select_xrange( 'delete', <axes handle> )
     <axes handle> で指定されるグラフを選択対象から除外します。

   select_xrange( 'get', <axes handle> )
     <axes handle> で指定されるグラフをにおける現在の選択範囲を返
     します。出力形式は [ 下界値, 上界値 ] です。

   select_xrange( 'clear', <axes handle> )
     <axes handle> で指定されるグラフの選択範囲を消去します。
   
   select_xrange( 'SetBoxColor', <axes handle>, <color> );
     <axes handle> で指定されるグラフにおける選択範囲を示すBOXの色
     を設定します。<color>はMatlabのColorSpecに準拠します。デフォ
     ルト値は [ 1.0, 0.6, 0.6 ] です。

   select_xrange( 'SetReturnVariable', <axes_handle>, <variable name> );
     <axes handle> で指定されるグラフにおける選択範囲を出力するた
     めのグローバル変数名を設定します。<variable name> は変数名を
     示す文字列になります。デフォルトは空行列です。

   select_xrange( 'SetButtonUpFcn', <axes_handle>, <command> );
     <axes handle> で指定されるグラフにおけるドラッグ終了時に実行
     するコマンドを設定します。ここで、選択した範囲を利用するには、
     xrange という変数名を用いて下さい。例えば、
       >> select_xrange( 'SetButtonUpFcn', 'func( xrange, var )' );
     などにより、選択範囲が関数funcの引数として渡されます。デフォ
     ルトは空行列です。

   select_xrange( 'SetInitialRange', <range> );
     <axes handle> で指定されるグラフにおける選択範囲の初期値を設
     定します。これを用いることにより、マウスドラッグ以外での範囲
     指定が可能です。<range>は2要素のベクトルで、それぞれx軸範囲の
     上界値と下界値を示します。マウスドラッグでは、表示グラフより
     大きい範囲指定はできませんが、ここでは可能です。デフォルトは
     空行列です。


 [ 簡単な使い方の説明 ]

   まず、選択対象となるグラフの axes handle を設定します。

   >> select_xrange( 'set', <axes handle> );

   これにより、対象グラフをマウスでドラッグすると
   選択されたx軸範囲が四角形で囲まれます。
   選択範囲にアクセスするには、

   >> select_xrange( 'get', <axes handle> );

   としてください。


 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = select_xrange(key, varargin)
0002 % select_xrange( key, varargin )
0003 %
0004 %  2次元グラフのx軸の範囲をマウスドラッグで指定するための
0005 %  コールバックルーチンです
0006 %
0007 % key :  'init'  'delete'  'get'  'clear'  'SetBoxColor'
0008 %        'SetReturnVariable'  'SetButtonUpFcn'  'SetInitialRange'
0009 %
0010 %   select_xrange( 'init', <axes handle>
0011 %       [ , 'BoxColor', <color> ]
0012 %       [ , 'ReturnVariable', <variable name> ]
0013 %       [ , 'ButtonUpFcn', <function string> ]
0014 %       [ , 'InitialRange', <range> ] )
0015 %     <axes handle> で指定されるグラフを選択対象に設定します。オプ
0016 %     ションとして、'BoxColor', 'BoxStyle',
0017 %     'ReturnVariable', 'ButtonUpFcn', 'InitialRange' を設定できま
0018 %     す。それぞれのオプションの詳細は下記を参照してください。
0019 %
0020 %   select_xrange( 'delete', <axes handle> )
0021 %     <axes handle> で指定されるグラフを選択対象から除外します。
0022 %
0023 %   select_xrange( 'get', <axes handle> )
0024 %     <axes handle> で指定されるグラフをにおける現在の選択範囲を返
0025 %     します。出力形式は [ 下界値, 上界値 ] です。
0026 %
0027 %   select_xrange( 'clear', <axes handle> )
0028 %     <axes handle> で指定されるグラフの選択範囲を消去します。
0029 %
0030 %   select_xrange( 'SetBoxColor', <axes handle>, <color> );
0031 %     <axes handle> で指定されるグラフにおける選択範囲を示すBOXの色
0032 %     を設定します。<color>はMatlabのColorSpecに準拠します。デフォ
0033 %     ルト値は [ 1.0, 0.6, 0.6 ] です。
0034 %
0035 %   select_xrange( 'SetReturnVariable', <axes_handle>, <variable name> );
0036 %     <axes handle> で指定されるグラフにおける選択範囲を出力するた
0037 %     めのグローバル変数名を設定します。<variable name> は変数名を
0038 %     示す文字列になります。デフォルトは空行列です。
0039 %
0040 %   select_xrange( 'SetButtonUpFcn', <axes_handle>, <command> );
0041 %     <axes handle> で指定されるグラフにおけるドラッグ終了時に実行
0042 %     するコマンドを設定します。ここで、選択した範囲を利用するには、
0043 %     xrange という変数名を用いて下さい。例えば、
0044 %       >> select_xrange( 'SetButtonUpFcn', 'func( xrange, var )' );
0045 %     などにより、選択範囲が関数funcの引数として渡されます。デフォ
0046 %     ルトは空行列です。
0047 %
0048 %   select_xrange( 'SetInitialRange', <range> );
0049 %     <axes handle> で指定されるグラフにおける選択範囲の初期値を設
0050 %     定します。これを用いることにより、マウスドラッグ以外での範囲
0051 %     指定が可能です。<range>は2要素のベクトルで、それぞれx軸範囲の
0052 %     上界値と下界値を示します。マウスドラッグでは、表示グラフより
0053 %     大きい範囲指定はできませんが、ここでは可能です。デフォルトは
0054 %     空行列です。
0055 %
0056 %
0057 % [ 簡単な使い方の説明 ]
0058 %
0059 %   まず、選択対象となるグラフの axes handle を設定します。
0060 %
0061 %   >> select_xrange( 'set', <axes handle> );
0062 %
0063 %   これにより、対象グラフをマウスでドラッグすると
0064 %   選択されたx軸範囲が四角形で囲まれます。
0065 %   選択範囲にアクセスするには、
0066 %
0067 %   >> select_xrange( 'get', <axes handle> );
0068 %
0069 %   としてください。
0070 %
0071 %
0072 % Copyright (C) 2011, ATR All Rights Reserved.
0073 % License : New BSD License(see VBMEG_LICENSE.txt)
0074 
0075 
0076 
0077 
0078 switch key
0079 
0080  % -----------------------------------------------------------------
0081  %  key : init
0082  % -----------------------------------------------------------------
0083  case 'init'  % axes を select_xrange の対象として設定
0084 
0085   % axes handle 取得
0086   h = varargin{1};
0087   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0088     disp( 'error in select_xrange: invalid axes handle' );
0089     return;
0090   end
0091 
0092   % 引数チェック(不完全)  %%%
0093   if mod( size(varargin,2), 2 ) ~= 1
0094     disp( 'invalid arguments for select_xrange( ''set'', ... )' );
0095     return;
0096   end 
0097   i = 2;
0098   while i < size(varargin,2)
0099     switch varargin{i}
0100      case 'BoxColor'
0101       boxcolor = varargin{i+1};
0102      case 'ReturnVariable'
0103       retvar = varargin{i+1};
0104      case 'ButtonUpFcn'
0105       upfcn = varargin{i+1};
0106      case 'InitialRange'
0107       xrange = varargin{i+1};
0108      otherwise
0109       disp( 'invalid arguments for select_xrange( ''set'', ... )' );
0110       return;
0111     end
0112     i = i + 2;
0113   end
0114   % ---- 引数チェック終了
0115   
0116   % オプションのデフォルト設定
0117   if ~exist('boxcolor'); boxcolor = [0.6,0.6,0.6]; end
0118   if ~exist('retvar'); retvar = []; end
0119   if ~exist('upfcn'); upfcn = []; end
0120   if ~exist('xrange'); xrange = []; end
0121   
0122   % axes に マウスボタンを押したときのコールバックを設定
0123   set( h, 'ButtonDownFcn',...
0124       [ 'select_xrange(''down'',''' retvar ''',''' upfcn ''');' ] );
0125 
0126   % 既存の範囲選択BOXがあれば削除
0127   boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0128   if ~isempty( boxhandle );
0129     delete( boxhandle );
0130   end
0131   
0132   boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0133   if ~isempty( boxhandle );
0134     delete( boxhandle );
0135   end
0136   
0137   % 新規に範囲選択BOXを作成
0138   ylim = get( h, 'YLim' );
0139   if isempty(xrange)
0140     xlim = get( h, 'XLim' );
0141     xinf = xlim(1);
0142     xsup = xlim(2);
0143   else
0144     xinf = xrange(1);
0145     xsup = xrange(2);
0146   end
0147   boxhandle = ...
0148       patch( [ xinf, xsup, xsup, xinf ],...
0149          [ ylim(1), ylim(1), ylim(2), ylim(2) ], ...
0150          boxcolor, 'HitTest', 'off', 'Visible', 'off',...
0151          'EraseMode', 'xor', 'Tag', 'SelX_BOX' );
0152   if ~isempty(xrange)
0153     set( boxhandle, 'Visible', 'on' );
0154   end
0155   % -----
0156 
0157 
0158 
0159  % -----------------------------------------------------------------
0160  %  key : delete
0161  % -----------------------------------------------------------------
0162  case 'delete'  % axes を select_xrange の対象から削除
0163   
0164   % axes handle 取得
0165   h = varargin{1};
0166   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0167     disp( 'error in select_xrange: invalid axes handle' );
0168     return;
0169   end
0170 
0171   % 範囲選択BOXがあれば削除 (BOXは'init'時に必ず作成される)
0172   boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0173   if ~isempty( boxhandle );
0174     delete( boxhandle );
0175     set( h, 'ButtonDownFcn', [] );
0176   end
0177   
0178   boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0179   if ~isempty( boxhandle );
0180     delete( boxhandle );
0181     set( h, 'ButtonDownFcn', [] );
0182   end
0183 
0184 
0185 
0186  % -----------------------------------------------------------------
0187  %  key : down
0188  % -----------------------------------------------------------------
0189  case 'down' % マウスボタンが押されたとき (Boxの描画,
0190              % WindowButtonMotionFcn, WindowButtonUpFcn の設定)
0191 
0192   % 引数 : retvar --- 出力用グローバル変数,
0193   %        upfcn --- マウスボタンを離したときのコマンド
0194   retvar = varargin{1};
0195   upfcn = varargin{2};
0196 
0197   % 有効なのは左クリックのみ
0198   if strcmp( get( gcf, 'SelectionType' ), 'normal' ) == 0
0199     return;
0200   end
0201   
0202   % BOXの再描画
0203   acpt = get( gca, 'CurrentPoint' );  
0204   xlim = get( gca, 'XLim' );
0205   ylim = get( gca, 'YLim' );
0206   xini = max( xlim(1), acpt(1) ); % 範囲選択は
0207   xini = min( xlim(2), xini );    % グラフのaxis内のみ
0208   boxhandle = findobj( gca, 'Tag', 'SelX_BOX' ); % BOXのハンドルを取得
0209   set( boxhandle, 'XData', [ xini, xini, xini, xini ],...
0210           'Visible', 'on' );
0211   
0212   % マウスを動かしたときとボタンを離したときのコールバックを設定
0213   set( gcf, 'WindowButtonMotionFcn', 'select_xrange(''move'')' );
0214   if isempty( retvar )
0215     set( gcf, 'WindowButtonUpFcn',...
0216           [ 'select_xrange(''up'',''' upfcn ''');' ] );
0217   else
0218     set( gcf, 'WindowButtonUpFcn',...
0219           [ retvar ' = select_xrange(''up'',''' upfcn ''');' ] );
0220   end
0221   
0222  
0223 
0224  % -----------------------------------------------------------------
0225  %  key : move
0226  % -----------------------------------------------------------------
0227  case 'move' % マウスを動かしたとき
0228 
0229   % BOXの再描画
0230   acpt = get( gca, 'CurrentPoint' );
0231   xlim = get( gca, 'XLim' );
0232   xcur = max( xlim(1), acpt(1) ); % 範囲選択は
0233   xcur = min( xlim(2), xcur );    % グラフのaxis内のみ
0234   boxhandle = findobj( gca, 'Tag', 'SelX_BOX' ); % BOXのハンドルを取得
0235   temp = get( boxhandle, 'XData' );
0236   xini = temp(1);
0237   set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0238     
0239 
0240 
0241  % -----------------------------------------------------------------
0242  %  key : up
0243  % -----------------------------------------------------------------
0244  case 'up' % マウスボタンを離したとき
0245 
0246   % 引数 : upfcn --- マウスボタンを離したときのコマンド
0247   upfcn = varargin{1};
0248   
0249   % マウスを動かしたときとマウスボタンを離したときのコールバックの解除
0250   set( gcf, 'WindowButtonMotionFcn', [] );
0251   set( gcf, 'WindowButtonUpFcn', [] );
0252   
0253   % BOXの再描画
0254   acpt = get( gca, 'CurrentPoint' );
0255   xlim = get( gca, 'XLim' );
0256   xcur = max( xlim(1), acpt(1) );
0257   xcur = min( xlim(2), xcur );
0258   boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0259   temp = get( boxhandle, 'XData' );
0260   xini = temp(1);
0261   set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0262   
0263   % 選択範囲
0264   xrange = sort( [ xini, xcur ] );
0265   
0266   % ユーザ指定関数呼び出し
0267   if ~isempty( upfcn )
0268     eval( upfcn );
0269   end
0270   
0271   % 返値として出力
0272   varargout{1} = xrange;
0273   
0274 
0275  
0276  % -----------------------------------------------------------------
0277  %  key : get
0278  % -----------------------------------------------------------------
0279  case 'get' % 指定範囲の取得
0280   
0281   % axes handle 取得
0282   h = varargin{1};
0283   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0284     disp( 'error in select_xrange: invalid axes handle' );
0285     varargout{1} = [];
0286     return;
0287   end
0288   
0289   % BOXのハンドルを取得 → あれば出力
0290   boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0291   if ~isempty(boxhandle) &...
0292      strcmp( get( boxhandle, 'Visible' ), 'on' ) == 1;
0293     xdat = get( boxhandle, 'XData' );
0294     xrange = sort( [ xdat(1), xdat(2) ] );
0295     varargout{1} = xrange;
0296   else
0297     varargout{1} = [];
0298   end
0299   
0300 
0301 
0302  % -----------------------------------------------------------------
0303  %  key : clear
0304  % -----------------------------------------------------------------
0305  case 'clear'  % BOXの消去
0306   
0307   % axes handle 取得
0308   h = varargin{1};
0309   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0310     disp( 'error in select_xrange: invalid axes handle' );
0311     return;
0312   end
0313 
0314   % BOXのハンドルを取得 → あれば非可視化
0315   boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0316   if ~isempty(boxhandle)
0317     xlim = get( h, 'XLim' );
0318     set( boxhandle, 'Visible', 'off' );
0319   end
0320 
0321 
0322 
0323  % -----------------------------------------------------------------
0324  %  key : SetBoxColor
0325  % -----------------------------------------------------------------
0326  case 'SetBoxColor'  % 範囲選択BOXの色を設定 (不完全) %%%
0327   
0328   % axes handle 取得
0329   h = varargin{1};
0330   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0331     disp( 'error in select_xrange: invalid axes handle' );
0332     return;
0333   end
0334   
0335   % 引数 : 設定する色 (チェック必要)
0336   boxcolor = varargin{2};
0337 
0338   % BOXのハンドルを取得 → あれば色の変更(不完全) %%%
0339   boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0340   if ~isempty(boxhandle)
0341     set( boxhandle, 'FaceVertexCData', boxcolor );
0342   end
0343   
0344 
0345 
0346  % -----------------------------------------------------------------
0347  %  key : SetReturnVariable
0348  % -----------------------------------------------------------------
0349  case 'SetReturnVariable' % ボタンを離したときに選択範囲を出力する
0350                           % ためのグローバル変数の設定
0351 
0352   % axes handle 取得
0353   h = varargin{1};
0354   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0355     disp( 'error in select_xrange: invalid axes handle' );
0356     return;
0357   end
0358 
0359   % 引数 : 設定する変数 (チェック必要?不可能か?) %%%
0360   retvar = varargin{2};
0361 
0362   % axes ButtonDownFcn コールバックの書き直し
0363   bdf = get( h, 'ButtonDownFcn' );
0364   i = findstr( bdf, 'select_xrange' );
0365   if ~isempty( i )
0366     c = findstr( bdf, ',' );
0367     set( h, 'ButtonDownFcn',...
0368         [ bdf(1:c(1)+1), retvar, bdf(c(2)-1:size(bdf,2)) ] );
0369   end
0370   
0371   
0372 
0373  % -----------------------------------------------------------------
0374  %  key : SetButtonUpFcn
0375  % -----------------------------------------------------------------
0376  case 'SetButtonUpFcn'  % ボタンを離したときに実行されるコマンドの設定
0377 
0378   % axes handle 取得
0379   h = varargin{1};
0380   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0381     disp( 'error in select_xrange: invalid axes handle' );
0382     return;
0383   end
0384 
0385   % 引数 : 設定するコマンド (チェック必要?不可能か?) %%%
0386   upfcn = varargin{2};
0387   
0388   % axes ButtonDownFcn コールバックの書き直し
0389   bdf = get( h, 'ButtonDownFcn' );
0390   i = findstr( bdf, 'select_xrange' );
0391   if ~isempty( i )
0392     c = findstr( bdf, ',' );
0393     set( h, 'ButtonDownFcn',...
0394         [ bdf(1:c(2)+1), upfcn, ''');' ] );
0395   end
0396 
0397 
0398  % -----------------------------------------------------------------
0399  %  key : SetInitialRange
0400  % -----------------------------------------------------------------
0401  case 'SetInitialRange'  % マウスドラッグ前の選択範囲を設定
0402   
0403   % axes handle 取得
0404   h = varargin{1};
0405   if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0406     disp( 'error in select_xrange: invalid axes handle' );
0407     return;
0408   end
0409   
0410   % 引数 : 設定する範囲 (チェック必要) %%%
0411   xrange = varargin{2};
0412   
0413   % BOXのハンドル取得 → あれば再描画&可視化
0414   boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0415   if ~isempty(boxhandle)
0416     set( boxhandle, 'XData', [xrange(1),xrange(2),xrange(2),xrange(1)],...
0417             'Visible', 'on');
0418   end
0419 
0420 
0421   
0422  % -----------------------------------------------------------------
0423  %  key : その他 (エラー)
0424  % -----------------------------------------------------------------
0425  otherwise
0426   disp( 'Invalid key for select_xrange()' );
0427 
0428 
0429   
0430 end % switch key
0431

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005