0001 function varargout = vb_select_xyrange(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 const = vb_define_verbose;
0078 VERBOSE_LEVEL_DEBUG = const.VERBOSE_LEVEL_DEBUG;
0079
0080 switch key
0081
0082
0083
0084
0085 case 'init'
0086
0087
0088 h = varargin{1};
0089 if ~ishandle(h),
0090 disp( 'error in vb_select_xyrange: invalid object handle' );
0091 return;
0092 end
0093
0094
0095
0096 hh = h;
0097 while true
0098 if strcmp(get(hh,'Type'),'axes'), break; end
0099 hh = get(hh,'Parent');
0100 end
0101
0102
0103 if mod( size(varargin,2), 2 ) ~= 1
0104 disp( 'invalid arguments for vb_select_xyrange( ''set'', ... )' );
0105 return;
0106 end
0107 i = 2;
0108 while i < size(varargin,2)
0109 switch varargin{i}
0110 case 'BoxColor'
0111 boxcolor = varargin{i+1};
0112 case 'ReturnVariable'
0113 retvar = varargin{i+1};
0114 case 'ButtonUpFcn'
0115 upfcn = varargin{i+1};
0116 case 'InitialRange'
0117 xyrange = varargin{i+1};
0118 otherwise
0119 disp( 'invalid arguments for vb_select_xyrange( ''set'', ... )' );
0120 return;
0121 end
0122 i = i + 2;
0123 end
0124
0125
0126
0127 if ~exist('boxcolor'); boxcolor = [0.6,0.6,0.6]; end
0128 if ~exist('retvar'); retvar = []; end
0129 if ~exist('upfcn'); upfcn = []; end
0130 if ~exist('xyrange'); xyrange = []; end
0131
0132
0133 set( h, 'ButtonDownFcn',...
0134 [ 'vb_select_xyrange(''down'',''' retvar ''',''' upfcn ''');' ] );
0135
0136
0137 boxhandle = findobj( hh, 'Tag', 'SelX_BOX' );
0138 if ~isempty( boxhandle );
0139 delete( boxhandle );
0140 end
0141
0142 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0143 if ~isempty( boxhandle );
0144 delete( boxhandle );
0145 end
0146
0147
0148 if isempty(xyrange)
0149 xlim = get( hh, 'XLim' );
0150 ylim = get( hh, 'YLim' );
0151 xinf = xlim(1);
0152 xsup = xlim(2);
0153 yinf = ylim(1);
0154 ysup = ylim(2);
0155 else
0156 xinf = xyrange(1);
0157 xsup = xyrange(3);
0158 yinf = xyrange(2);
0159 ysup = xyrange(4);
0160 end
0161
0162 set(gcf, 'CurrentAxes', hh);
0163 boxhandle = ...
0164 patch( [ xinf, xsup, xsup, xinf ],...
0165 [ yinf, yinf, ysup, ysup ], ...
0166 boxcolor, 'HitTest', 'off', 'Visible', 'off',...
0167 'Tag', 'SelXY_BOX' , ...
0168 'FaceColor', 'none', 'LineWidth', 2.5);
0169 if ~isempty(xyrange)
0170 set( boxhandle, 'Visible', 'on' );
0171 end
0172
0173
0174
0175
0176
0177
0178
0179 case 'delete'
0180
0181
0182 h = varargin{1};
0183 if ~ishandle(h),
0184 disp( 'error in vb_select_xyrange: invalid object handle' );
0185 return;
0186 end
0187
0188
0189
0190 hh = h;
0191 while true
0192 if strcmp(get(hh,'Type'),'axes'), break; end
0193 hh = get(hh,'Parent');
0194 end
0195
0196
0197 boxhandle = findobj( hh, 'Tag', 'SelX_BOX' );
0198 if ~isempty( boxhandle );
0199 delete( boxhandle );
0200 set( h, 'ButtonDownFcn', [] );
0201 end
0202
0203 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0204 if ~isempty( boxhandle );
0205 delete( boxhandle );
0206 set( h, 'ButtonDownFcn', [] );
0207 end
0208
0209
0210
0211
0212
0213
0214 case 'down'
0215
0216
0217
0218
0219 retvar = varargin{1};
0220 upfcn = varargin{2};
0221
0222
0223 if vb_get_verbose==VERBOSE_LEVEL_DEBUG,
0224 disp('vb_select_xyrange(''down'') invoked');
0225 fprintf('upfcn=%s\n',upfcn);
0226 end
0227
0228
0229 if strcmp( get( gcf, 'SelectionType' ), 'normal' ) == 0
0230 return;
0231 end
0232
0233
0234 acpt = get( gca, 'CurrentPoint' );
0235 xlim = get( gca, 'XLim' );
0236 ylim = get( gca, 'YLim' );
0237 xini = max( xlim(1), acpt(1) );
0238 xini = min( xlim(2), xini );
0239 yini = max( ylim(1), acpt(3) );
0240 yini = min( ylim(2), yini );
0241 boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0242 set( boxhandle, 'XData', [ xini, xini, xini, xini ],...
0243 'YData', [ yini, yini, yini, yini ],...
0244 'Visible', 'on' );
0245
0246
0247 set( gcf, 'WindowButtonMotionFcn', 'vb_select_xyrange(''move'')' );
0248 if isempty( retvar )
0249 set( gcf, 'WindowButtonUpFcn',...
0250 [ 'vb_select_xyrange(''up'',''' upfcn ''');' ] );
0251 else
0252 set( gcf, 'WindowButtonUpFcn',...
0253 [ retvar ' = vb_select_xyrange(''up'',''' upfcn ''');' ] );
0254 end
0255
0256
0257
0258
0259
0260
0261 case 'move'
0262
0263
0264 acpt = get( gca, 'CurrentPoint' );
0265 xlim = get( gca, 'XLim' );
0266 ylim = get( gca, 'YLim' );
0267 xcur = max( xlim(1), acpt(1) );
0268 xcur = min( xlim(2), xcur );
0269 ycur = max( ylim(1), acpt(3) );
0270 ycur = min( ylim(2), ycur );
0271 boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0272 temp = get( boxhandle, 'XData' );
0273 xini = temp(1);
0274 set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0275 temp = get( boxhandle, 'YData' );
0276 yini = temp(1);
0277 set( boxhandle, 'YData', [ yini, yini, ycur, ycur ] );
0278
0279
0280 if vb_get_verbose==VERBOSE_LEVEL_DEBUG,
0281 disp('vb_select_xyrange(''move'') invoked');
0282 fprintf('xyrange=[%f %f %f %f]\n',xini,xcur,yini,ycur);
0283 end
0284
0285
0286
0287
0288 case 'up'
0289
0290
0291 upfcn = varargin{1};
0292
0293
0294 set( gcf, 'WindowButtonMotionFcn', [] );
0295 set( gcf, 'WindowButtonUpFcn', [] );
0296
0297
0298 acpt = get( gca, 'CurrentPoint' );
0299 xlim = get( gca, 'XLim' );
0300 ylim = get( gca, 'YLim' );
0301 xcur = max( xlim(1), acpt(1) );
0302 xcur = min( xlim(2), xcur );
0303 ycur = max( ylim(1), acpt(3) );
0304 ycur = min( ylim(2), ycur );
0305 boxhandle = findobj( gca, 'Tag', 'SelXY_BOX' );
0306 temp = get( boxhandle, 'XData' );
0307 xini = temp(1);
0308 set( boxhandle, 'XData', [ xini, xcur, xcur, xini ] );
0309 temp = get( boxhandle, 'YData' );
0310 yini = temp(1);
0311 set( boxhandle, 'YData', [ yini, yini, ycur, ycur ] );
0312
0313
0314 xrange = sort( [ xini, xcur ] );
0315 yrange = sort( [ yini, ycur ] );
0316 xyrange = [xrange(1) yrange(1) xrange(2) yrange(2)];
0317
0318
0319 if ~isempty( upfcn )
0320 eval( upfcn );
0321 end
0322
0323
0324 varargout{1} = xyrange;
0325
0326
0327
0328
0329
0330
0331 case 'get'
0332
0333
0334 h = varargin{1};
0335 if ~ishandle(h),
0336 disp( 'error in vb_select_xyrange: invalid object handle' );
0337 varargout{1} = [];
0338 return;
0339 end
0340
0341
0342
0343 hh = h;
0344 while true
0345 if strcmp(get(hh,'Type'),'axes'), break; end
0346 hh = get(hh,'Parent');
0347 end
0348
0349
0350 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0351 if ~isempty(boxhandle) &...
0352 strcmp( get( boxhandle, 'Visible' ), 'on' ) == 1;
0353 xdat = get( boxhandle, 'XData' );
0354 ydat = get( boxhandle, 'YData' );
0355 xrange = sort( [ xdat(1), xdat(2) ] );
0356 yrange = sort( [ ydat(1), ydat(3) ] );
0357 xyrange = [xrange(1) yrange(1) xrange(2) yrange(2)];
0358 varargout{1} = xyrange;
0359 else
0360 varargout{1} = [];
0361 end
0362
0363
0364
0365
0366
0367
0368 case 'clear'
0369
0370
0371 h = varargin{1};
0372 if ~ishandle(h),
0373 disp( 'error in vb_select_xyrange: invalid object handle' );
0374 return;
0375 end
0376
0377
0378
0379 hh = h;
0380 while true
0381 if strcmp(get(hh,'Type'),'axes'), break; end
0382 hh = get(hh,'Parent');
0383 end
0384
0385
0386 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0387 if ~isempty(boxhandle)
0388 xlim = get( hh, 'XLim' );
0389 set( boxhandle, 'Visible', 'off' );
0390 end
0391
0392
0393
0394
0395
0396
0397 case 'SetBoxColor'
0398
0399
0400 h = varargin{1};
0401 if ~ishandle(h),
0402 disp( 'error in vb_select_xyrange: invalid object handle' );
0403 return;
0404 end
0405
0406
0407
0408 hh = h;
0409 while true
0410 if strcmp(get(hh,'Type'),'axes'), break; end
0411 hh = get(hh,'Parent');
0412 end
0413
0414
0415 boxcolor = varargin{2};
0416
0417
0418 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0419 if ~isempty(boxhandle)
0420 set( boxhandle, 'FaceVertexCData', boxcolor );
0421 end
0422
0423
0424
0425
0426
0427
0428 case 'SetReturnVariable'
0429
0430
0431
0432 h = varargin{1};
0433 if ~ishandle(h),
0434 disp( 'error in vb_select_xyrange: invalid object handle' );
0435 return;
0436 end
0437
0438
0439 retvar = varargin{2};
0440
0441
0442 bdf = get( h, 'ButtonDownFcn' );
0443 i = findstr( bdf, 'vb_select_xyrange' );
0444 if ~isempty( i )
0445 c = findstr( bdf, ',' );
0446 set( h, 'ButtonDownFcn',...
0447 [ bdf(1:c(1)+1), retvar, bdf(c(2)-1:size(bdf,2)) ] );
0448 end
0449
0450
0451
0452
0453
0454
0455 case 'SetButtonUpFcn'
0456
0457
0458 h = varargin{1};
0459 if ~ishandle(h),
0460 disp( 'error in vb_select_xyrange: invalid object handle' );
0461 return;
0462 end
0463
0464
0465 upfcn = varargin{2};
0466
0467
0468 bdf = get( h, 'ButtonDownFcn' );
0469 i = findstr( bdf, 'vb_select_xyrange' );
0470 if ~isempty( i )
0471 c = findstr( bdf, ',' );
0472 set( h, 'ButtonDownFcn',...
0473 [ bdf(1:c(2)+1), upfcn, ''');' ] );
0474 end
0475
0476
0477
0478
0479
0480 case 'SetInitialRange'
0481
0482
0483 h = varargin{1};
0484 if ~ishandle(h),
0485 disp( 'error in vb_select_xyrange: invalid object handle' );
0486 return;
0487 end
0488
0489
0490
0491 hh = h;
0492 while true
0493 if strcmp(get(hh,'Type'),'axes'), break; end
0494 hh = get(hh,'Parent');
0495 end
0496
0497
0498 xyrange = varargin{2};
0499
0500
0501 boxhandle = findobj( hh, 'Tag', 'SelXY_BOX' );
0502 if ~isempty(boxhandle)
0503 set( boxhandle, 'XData', [xrange(1),xrange(3),xrange(3),xrange(1)],...
0504 'YData', [xrange(2),xrange(2),xrange(4),xrange(4)],...
0505 'Visible', 'on');
0506 end
0507
0508
0509
0510
0511
0512
0513 otherwise
0514 disp( 'Invalid key for vb_select_xyrange()' );
0515
0516
0517
0518 end
0519