Robochameleon  v1.0
gridxy.m
1 function hh = gridxy(x,varargin)
2 % GRIDXY - Plot grid lines
3 % GRIDXY(X) plots vertical grid lines at the positions specified
4 % by X. GRIDXY(X,Y) also plots horizontal grid lines at the positions
5 % specified by Y. GRIDXY uses the current axes, if any. Lines outside
6 % the plot area are plotted but not shown. When X or Y is empty no vertical
7 % or horizontal lines are plotted.
8 %
9 % The lines are plotted as a single graphics object. H = GRIDXY(..) returns
10 % a graphics handle to that line object.
11 %
12 % GRIDXY(..., 'Prop1','Val1','Prop2','Val2', ...) uses the properties
13 % and values specified for color, linestyle, etc. Execute GET(H), where H is
14 % a line handle, to see a list of line object properties and their current values.
15 % Execute SET(H) to see a list of line object properties and legal property values.
16 %
17 % Examples
18 % % some random plot
19 % plot(10*rand(100,1), 10*rand(100,1),'bo') ;
20 % % horizontal red dashed grid
21 % gridxy([1.1 3.2 4.5],'Color','r','Linestyle',':') ;
22 % % vertical solid thicker yellowish grid, and store the handle
23 % h = gridxy([],[2.1:0.7:5 8],'Color',[0.9 1.0 0.2],'linewidth',3) ;
24 %
25 % GRIDXY can be used to plot a irregular grid on the axes.
26 %
27 % See also PLOT, REFLINE, GRID, AXES, REFLINEXY
28 
29 % NOTE: This function was previously known as XYREFLINE
30 
31 % for Matlab R13
32 % version 2.2 (feb 2008)
33 % (c) Jos van der Geest
34 % email: jos@jasen.nl
35 
36 % History
37 % Created (1.0) feb 2006
38 % 2.0 apr 2007 - renamed from reflinexy to gridxy, reflinexy is now used
39 % for plotting intersection between X and Y axes
40 % 2.1 apr 2007 - add error check for line properties
41 % 2.2 feb 2008 - added set(gca,'layer','top') to put gridlines behind the
42 % axis tick marks
43 
44 narginchk(1,Inf);
45 
46 % check the arguments
47 if ~isnumeric(x),
48  error('Numeric argument expected') ;
49 end
50 
51 if nargin==1,
52  y = [] ;
53  va = [] ;
54 else
55  va = varargin ;
56  if ischar(va{1}),
57  % optional arguments are
58  y = [] ;
59  elseif isnumeric(va{1})
60  y = va{1} ;
61  va = va(2:end) ;
62  else
63  error('Invalid second argument') ;
64  end
65  if mod(size(va),2) == 1,
66  error('Property-Value have to be pairs') ;
67  end
68 end
69 
70 % get the axes to plot in
71 hca=get(get(0,'currentfigure'),'currentaxes');
72 if isempty(hca),
73  warning('No current axes found') ;
74  return ;
75 end
76 
77 % get the current limits of the axis
78 % used for limit restoration later on
79 xlim = get(hca,'xlim') ;
80 ylim = get(hca,'ylim') ;
81 
82 % setup data for the vertical lines
83 xx1 = repmat(x(:).',3,1) ;
84 yy1 = repmat([ylim(:) ; nan],1,numel(x)) ;
85 
86 % setup data for the horizontal lines
87 xx2 = repmat([xlim(:) ; nan],1,numel(y)) ;
88 yy2 = repmat(y(:).',3,1) ;
89 
90 
91 % create data for a single line object
92 xx1 = [xx1 xx2] ;
93 if ~isempty(xx1),
94  yy1 = [yy1 yy2] ;
95  % add the line to the current axes
96  np = get(hca,'nextplot') ;
97  set(hca,'nextplot','add') ;
98  h = line('xdata',xx1(:),'ydata',yy1(:)) ;
99  set(hca,'ylim',ylim,'xlim',xlim) ; % reset the limits
100 
101  uistack(h,'bottom') ; % push lines to the bottom of the graph
102  set(hca,'nextplot',np,'Layer','top') ; % reset the nextplot state
103 
104  if ~isempty(va),
105  try
106  set(h,va{:}) ; % set line properties
107  catch
108  msg = lasterror ;
109  error(msg.message(21:end)) ;
110  end
111  end
112 
113 else
114  h = [] ;
115 end
116 
117 if nargout==1, % if requested return handle
118  hh = h ;
119 end
120 
121 
122 
123 
124 
125 
126 
127 
128