0001 function varargout = select_xrange(key, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 switch key
0079
0080
0081
0082
0083 case 'init'
0084
0085
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
0123 set( h, 'ButtonDownFcn',...
0124 [ 'select_xrange(''down'',''' retvar ''',''' upfcn ''');' ] );
0125
0126
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
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 if vb_matlab_version('<', '8.4.0')
0148 boxhandle = ...
0149 patch( [ xinf, xsup, xsup, xinf ],...
0150 [ ylim(1), ylim(1), ylim(2), ylim(2) ], ...
0151 boxcolor, 'HitTest', 'off', 'Visible', 'off',...
0152 'EraseMode', 'xor', 'Tag', 'SelX_BOX' );
0153 else
0154 boxhandle = ...
0155 patch( [ xinf, xsup, xsup, xinf ],...
0156 [ ylim(1), ylim(1), ylim(2), ylim(2) ], ...
0157 boxcolor, 'HitTest', 'off', 'Visible', 'off',...
0158 'Tag', 'SelX_BOX' );
0159 boxhandle.FaceAlpha = 0.5;
0160 end
0161 if ~isempty(xrange)
0162 set( boxhandle, 'Visible', 'on' );
0163 end
0164
0165
0166
0167
0168
0169
0170
0171 case 'delete'
0172
0173
0174 h = varargin{1};
0175 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0176 disp( 'error in select_xrange: invalid axes handle' );
0177 return;
0178 end
0179
0180
0181 boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0182 if ~isempty( boxhandle );
0183 delete( boxhandle );
0184 set( h, 'ButtonDownFcn', [] );
0185 end
0186
0187 boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0188 if ~isempty( boxhandle );
0189 delete( boxhandle );
0190 set( h, 'ButtonDownFcn', [] );
0191 end
0192
0193
0194
0195
0196
0197
0198 case 'down'
0199
0200
0201
0202
0203 retvar = varargin{1};
0204 upfcn = varargin{2};
0205
0206
0207 if strcmp( get( gcf, 'SelectionType' ), 'normal' ) == 0
0208 return;
0209 end
0210
0211
0212 acpt = get( gca, 'CurrentPoint' );
0213 xlim = get( gca, 'XLim' );
0214 ylim = get( gca, 'YLim' );
0215 xini = max( xlim(1), acpt(1) );
0216 xini = min( xlim(2), xini );
0217 boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0218 set( boxhandle, 'XData', [ xini, xini, xini, xini ],...
0219 'Visible', 'on' );
0220
0221
0222 set( gcf, 'WindowButtonMotionFcn', 'select_xrange(''move'')' );
0223 if isempty( retvar )
0224 set( gcf, 'WindowButtonUpFcn',...
0225 [ 'select_xrange(''up'',''' upfcn ''');' ] );
0226 else
0227 set( gcf, 'WindowButtonUpFcn',...
0228 [ retvar ' = select_xrange(''up'',''' upfcn ''');' ] );
0229 end
0230
0231
0232
0233
0234
0235
0236 case 'move'
0237
0238
0239 acpt = get( gca, 'CurrentPoint' );
0240 xlim = get( gca, 'XLim' );
0241 xcur = max( xlim(1), acpt(1) );
0242 xcur = min( xlim(2), xcur );
0243 boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0244 temp = get( boxhandle, 'XData' );
0245 xini = temp(1);
0246 set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0247
0248
0249
0250
0251
0252
0253 case 'up'
0254
0255
0256 upfcn = varargin{1};
0257
0258
0259 set( gcf, 'WindowButtonMotionFcn', [] );
0260 set( gcf, 'WindowButtonUpFcn', [] );
0261
0262
0263 acpt = get( gca, 'CurrentPoint' );
0264 xlim = get( gca, 'XLim' );
0265 xcur = max( xlim(1), acpt(1) );
0266 xcur = min( xlim(2), xcur );
0267 boxhandle = findobj( gca, 'Tag', 'SelX_BOX' );
0268 temp = get( boxhandle, 'XData' );
0269 xini = temp(1);
0270 set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0271
0272
0273 xrange = sort( [ xini, xcur ] );
0274
0275
0276 if ~isempty( upfcn )
0277 eval( upfcn );
0278 end
0279
0280
0281 varargout{1} = xrange;
0282
0283
0284
0285
0286
0287
0288 case 'get'
0289
0290
0291 h = varargin{1};
0292 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0293 disp( 'error in select_xrange: invalid axes handle' );
0294 varargout{1} = [];
0295 return;
0296 end
0297
0298
0299 boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0300 if ~isempty(boxhandle) &...
0301 strcmp( get( boxhandle, 'Visible' ), 'on' ) == 1;
0302 xdat = get( boxhandle, 'XData' );
0303 xrange = sort( [ xdat(1), xdat(2) ] );
0304 varargout{1} = xrange;
0305 else
0306 varargout{1} = [];
0307 end
0308
0309
0310
0311
0312
0313
0314 case 'clear'
0315
0316
0317 h = varargin{1};
0318 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0319 disp( 'error in select_xrange: invalid axes handle' );
0320 return;
0321 end
0322
0323
0324 boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0325 if ~isempty(boxhandle)
0326 xlim = get( h, 'XLim' );
0327 set( boxhandle, 'Visible', 'off' );
0328 end
0329
0330
0331
0332
0333
0334
0335 case 'SetBoxColor'
0336
0337
0338 h = varargin{1};
0339 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0340 disp( 'error in select_xrange: invalid axes handle' );
0341 return;
0342 end
0343
0344
0345 boxcolor = varargin{2};
0346
0347
0348 boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0349 if ~isempty(boxhandle)
0350 set( boxhandle, 'FaceVertexCData', boxcolor );
0351 end
0352
0353
0354
0355
0356
0357
0358 case 'SetReturnVariable'
0359
0360
0361
0362 h = varargin{1};
0363 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0364 disp( 'error in select_xrange: invalid axes handle' );
0365 return;
0366 end
0367
0368
0369 retvar = varargin{2};
0370
0371
0372 bdf = get( h, 'ButtonDownFcn' );
0373 i = findstr( bdf, 'select_xrange' );
0374 if ~isempty( i )
0375 c = findstr( bdf, ',' );
0376 set( h, 'ButtonDownFcn',...
0377 [ bdf(1:c(1)+1), retvar, bdf(c(2)-1:size(bdf,2)) ] );
0378 end
0379
0380
0381
0382
0383
0384
0385 case 'SetButtonUpFcn'
0386
0387
0388 h = varargin{1};
0389 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0390 disp( 'error in select_xrange: invalid axes handle' );
0391 return;
0392 end
0393
0394
0395 upfcn = varargin{2};
0396
0397
0398 bdf = get( h, 'ButtonDownFcn' );
0399 i = findstr( bdf, 'select_xrange' );
0400 if ~isempty( i )
0401 c = findstr( bdf, ',' );
0402 set( h, 'ButtonDownFcn',...
0403 [ bdf(1:c(2)+1), upfcn, ''');' ] );
0404 end
0405
0406
0407
0408
0409
0410 case 'SetInitialRange'
0411
0412
0413 h = varargin{1};
0414 if ~ishandle(h) | ~strcmp( get(h,'Type'), 'axes' )
0415 disp( 'error in select_xrange: invalid axes handle' );
0416 return;
0417 end
0418
0419
0420 xrange = varargin{2};
0421
0422
0423 boxhandle = findobj( h, 'Tag', 'SelX_BOX' );
0424 if ~isempty(boxhandle)
0425 set( boxhandle, 'XData', [xrange(1),xrange(2),xrange(2),xrange(1)],...
0426 'Visible', 'on');
0427 end
0428
0429
0430
0431
0432
0433
0434 otherwise
0435 disp( 'Invalid key for select_xrange()' );
0436
0437
0438
0439 end
0440