rri_xhair.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. % rri_xhair: create a pair of full_cross_hair at point [x y] in
  2. % axes h_ax, and return xhair struct
  3. %
  4. % Usage: xhair = rri_xhair([x y], xhair, h_ax);
  5. %
  6. % If omit xhair, rri_xhair will create a pair of xhair; otherwise,
  7. % rri_xhair will update the xhair. If omit h_ax, current axes will
  8. % be used.
  9. %
  10. % 24-nov-2003 jimmy (jimmy@rotman-baycrest.on.ca)
  11. %
  12. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13. function xhair = rri_xhair(varargin)
  14. if nargin == 0
  15. error('Please enter a point position as first argument');
  16. return;
  17. end
  18. if nargin > 0
  19. p = varargin{1};
  20. if ~isnumeric(p) | length(p) ~= 2
  21. error('Invalid point position');
  22. return;
  23. else
  24. xhair = [];
  25. end
  26. end
  27. if nargin > 1
  28. xhair = varargin{2};
  29. if ~isempty(xhair)
  30. if ~isstruct(xhair)
  31. error('Invalid xhair struct');
  32. return;
  33. elseif ~isfield(xhair,'lx') | ~isfield(xhair,'ly')
  34. error('Invalid xhair struct');
  35. return;
  36. elseif ~ishandle(xhair.lx) | ~ishandle(xhair.ly)
  37. error('Invalid xhair struct');
  38. return;
  39. end
  40. lx = xhair.lx;
  41. ly = xhair.ly;
  42. else
  43. lx = [];
  44. ly = [];
  45. end
  46. end
  47. if nargin > 2
  48. h_ax = varargin{3};
  49. if ~ishandle(h_ax)
  50. error('Invalid axes handle');
  51. return;
  52. elseif ~strcmp(lower(get(h_ax,'type')), 'axes')
  53. error('Invalid axes handle');
  54. return;
  55. end
  56. else
  57. h_ax = gca;
  58. end
  59. x_range = get(h_ax,'xlim');
  60. y_range = get(h_ax,'ylim');
  61. if ~isempty(xhair)
  62. set(lx, 'ydata', [p(2) p(2)]);
  63. set(ly, 'xdata', [p(1) p(1)]);
  64. set(h_ax, 'selected', 'on');
  65. set(h_ax, 'selected', 'off');
  66. else
  67. figure(get(h_ax,'parent'));
  68. axes(h_ax);
  69. xhair.lx = line('xdata', x_range, 'ydata', [p(2) p(2)], ...
  70. 'zdata', [11 11], 'color', [1 0 0], 'hittest', 'off');
  71. xhair.ly = line('xdata', [p(1) p(1)], 'ydata', y_range, ...
  72. 'zdata', [11 11], 'color', [1 0 0], 'hittest', 'off');
  73. end
  74. set(h_ax,'xlim',x_range);
  75. set(h_ax,'ylim',y_range);
  76. return;