comparison scripts/time/datetick.m @ 10369:3516a245d607

datetick.m: Fix 'keepticks' bug, and untabify.
author Ben Abbott <bpabbott@mac.com>
date Sat, 27 Feb 2010 23:04:09 -0500
parents c2f1cdb59821
children be55736a0783
comparison
equal deleted inserted replaced
10368:d2849dbcc858 10369:3516a245d607
53 %! plot (datenum (yr, 1, 1), pop); 53 %! plot (datenum (yr, 1, 1), pop);
54 %! title ("US population (millions)"); 54 %! title ("US population (millions)");
55 %! xlabel ("Year"); 55 %! xlabel ("Year");
56 %! datetick ("x", "YYYY"); 56 %! datetick ("x", "YYYY");
57 57
58 %!demo
59 %! yr =1988:2:2002;
60 %! yr =datenum(yr,1,1);
61 %! pr = [12.1 13.3 12.6 13.1 13.3 14.1 14.4 15.2];
62 %! plot(yr,pr);
63 %! xlabel('year')
64 %! ylabel('average price')
65 %! ax=gca;
66 %! set(ax,'xtick',datenum(1990:5:2005,1,1))
67 %! datetick(2,'keepticks')
68 %! set(ax,'ytick',12:16)
69
58 function __datetick__ (varargin) 70 function __datetick__ (varargin)
59 71
60 keeplimits = false; 72 keeplimits = false;
61 keeptick = false; 73 keepticks = false;
62 idx = []; 74 idx = [];
63 for i = 1 : nargin 75 for i = 1 : nargin
64 arg = varargin {i}; 76 arg = varargin {i};
65 if (ischar (arg)) 77 if (ischar (arg))
66 if (strcmpi (arg, "keeplimits")) 78 if (strcmpi (arg, "keeplimits"))
67 keeplimits = true; 79 keeplimits = true;
68 idx = [idx, i]; 80 idx = [idx, i];
69 elseif (strcmpi (arg, "keeptick")) 81 elseif (strcmpi (arg, "keepticks"))
70 keeptick = true; 82 keepticks = true;
71 idx = [idx, i]; 83 idx = [idx, i];
72 endif 84 endif
73 endif 85 endif
74 endfor 86 endfor
75 87
76 varargin(idx) = []; 88 varargin(idx) = [];
79 ax = "x"; 91 ax = "x";
80 92
81 if (nargin != 0) 93 if (nargin != 0)
82 arg = varargin{1}; 94 arg = varargin{1};
83 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") || 95 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") ||
84 strcmp (arg, "z"))) 96 strcmp (arg, "z")))
85 ax = arg; 97 ax = arg;
86 if (nargin > 1) 98 if (nargin > 1)
87 form = varargin{2}; 99 form = varargin{2};
88 varargin(1:2) = []; 100 varargin(1:2) = [];
89 else 101 else
90 varargin(1) = []; 102 varargin(1) = [];
91 endif 103 endif
92 else 104 else
93 form = arg; 105 form = arg;
94 varargin(1) = []; 106 varargin(1) = [];
95 endif 107 endif
110 elseif (! ischar (form)) 122 elseif (! ischar (form))
111 error ("datetick: expecting valid date format string"); 123 error ("datetick: expecting valid date format string");
112 endif 124 endif
113 endif 125 endif
114 126
115 if (keeptick) 127 if (keepticks)
116 ticks = get (gca (), strcat (ax, "tick")) 128 ticks = get (gca (), strcat (ax, "tick"))
117 else 129 else
118 ## Need to do our own axis tick position calculation as 130 ## Need to do our own axis tick position calculation as
119 ## year, etc, don't fallback on nice datenum values. 131 ## year, etc, don't fallback on nice datenum values.
120 objs = findall (gca()); 132 objs = findall (gca());
121 xmax = NaN; 133 xmax = NaN;
122 xmin = NaN; 134 xmin = NaN;
123 for i = 1 : length (objs) 135 for i = 1 : length (objs)
124 fld = get (objs (i)); 136 fld = get (objs (i));
125 if (isfield (fld, strcat (ax, "data"))) 137 if (isfield (fld, strcat (ax, "data")))
126 xdata = getfield (fld, strcat (ax, "data"))(:); 138 xdata = getfield (fld, strcat (ax, "data"))(:);
127 xmin = min (xmin, min (xdata)); 139 xmin = min (xmin, min (xdata));
128 xmax = max (xmax, max (xdata)); 140 xmax = max (xmax, max (xdata));
129 endif 141 endif
130 endfor 142 endfor
131 143
132 if (isnan (xmin) || isnan (xmax)) 144 if (isnan (xmin) || isnan (xmax))
133 xmin = 0; 145 xmin = 0;
138 150
139 N = 3; 151 N = 3;
140 if (xmax - xmin < N) 152 if (xmax - xmin < N)
141 ## Day scale or less 153 ## Day scale or less
142 if (xmax - xmin < N / 24 / 60 / 60) 154 if (xmax - xmin < N / 24 / 60 / 60)
143 scl = 1 / 24 / 60 / 60; 155 scl = 1 / 24 / 60 / 60;
144 elseif (xmax - xmin < N / 24 / 6) 156 elseif (xmax - xmin < N / 24 / 6)
145 scl = 1 / 24 / 60; 157 scl = 1 / 24 / 60;
146 else 158 else
147 scl = 1 / 24; 159 scl = 1 / 24;
148 endif 160 endif
149 sep = __calc_tick_sep__ (xmin / scl , xmax / scl); 161 sep = __calc_tick_sep__ (xmin / scl , xmax / scl);
150 xmin = sep * floor (xmin / scl / sep); 162 xmin = sep * floor (xmin / scl / sep);
151 xmax = sep * ceil (xmax / scl / sep); 163 xmax = sep * ceil (xmax / scl / sep);
152 nticks = (xmax - xmin) / sep + 1; 164 nticks = (xmax - xmin) / sep + 1;
159 maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30; 171 maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30;
160 minmonth = mmin + (dmin - 1) / 30; 172 minmonth = mmin + (dmin - 1) / 30;
161 maxmonth = (ymax - ymin) * 12 + mmax + (dmax - 1) / 30; 173 maxmonth = (ymax - ymin) * 12 + mmax + (dmax - 1) / 30;
162 174
163 if (maxmonth - minmonth < N) 175 if (maxmonth - minmonth < N)
164 sep = __calc_tick_sep__ (xmin, xmax); 176 sep = __calc_tick_sep__ (xmin, xmax);
165 xmin = sep * floor (xmin / sep); 177 xmin = sep * floor (xmin / sep);
166 xmax = sep * ceil (xmax / sep); 178 xmax = sep * ceil (xmax / sep);
167 nticks = (xmax - xmin) / sep + 1; 179 nticks = (xmax - xmin) / sep + 1;
168 elseif (maxyear - minyear < N) 180 elseif (maxyear - minyear < N)
169 sep = __calc_tick_sep__ (minmonth , maxmonth); 181 sep = __calc_tick_sep__ (minmonth , maxmonth);
170 xmin = datenum (ymin, sep * floor (minmonth / sep), 1); 182 xmin = datenum (ymin, sep * floor (minmonth / sep), 1);
171 xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1); 183 xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1);
172 nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1; 184 nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1;
173 else 185 else
174 sep = __calc_tick_sep__ (minyear , maxyear); 186 sep = __calc_tick_sep__ (minyear , maxyear);
175 xmin = datenum (sep * floor (minyear / sep), 1, 1); 187 xmin = datenum (sep * floor (minyear / sep), 1, 1);
176 xmax = datenum (sep * ceil (maxyear / sep), 1, 1); 188 xmax = datenum (sep * ceil (maxyear / sep), 1, 1);
177 nticks = ceil (maxyear / sep) - floor (minyear / sep) + 1; 189 nticks = ceil (maxyear / sep) - floor (minyear / sep) + 1;
178 endif 190 endif
179 endif 191 endif
180 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin); 192 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin);
181 endif 193 endif
182 194
207 219
208 if (length (ticks) == 6) 220 if (length (ticks) == 6)
209 ## Careful that its not treated as a datevec 221 ## Careful that its not treated as a datevec
210 if (! isempty (startdate)) 222 if (! isempty (startdate))
211 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form), 223 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form),
212 datestr (ticks(end) - ticks(1) + startdate, form)); 224 datestr (ticks(end) - ticks(1) + startdate, form));
213 else 225 else
214 sticks = strvcat (datestr (ticks(1:end-1), form), 226 sticks = strvcat (datestr (ticks(1:end-1), form),
215 datestr (ticks(end), form)); 227 datestr (ticks(end), form));
216 endif 228 endif
217 else 229 else
218 if (! isempty (startdate)) 230 if (! isempty (startdate))
219 sticks = datestr (ticks - ticks(1) + startdate, form); 231 sticks = datestr (ticks - ticks(1) + startdate, form);
220 else 232 else
222 endif 234 endif
223 endif 235 endif
224 236
225 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks)); 237 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks));
226 238
227 if (keeptick) 239 if (keepticks)
228 if (keeplimits) 240 if (keeplimits)
229 set (gca(), strcat (ax, "ticklabel"), sticks); 241 set (gca(), strcat (ax, "ticklabel"), sticks);
230 else 242 else
231 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"), 243 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"),
232 [min(ticks), max(ticks)]); 244 [min(ticks), max(ticks)]);
233 endif 245 endif
234 else 246 else
235 if (keeplimits) 247 if (keeplimits)
236 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks); 248 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks);
237 else 249 else
238 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks, 250 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks,
239 strcat (ax, "lim"), [min(ticks), max(ticks)]); 251 strcat (ax, "lim"), [min(ticks), max(ticks)]);
240 endif 252 endif
241 endif 253 endif
242 endfunction 254 endfunction
243 255
244 function [a, b] = __magform__ (x) 256 function [a, b] = __magform__ (x)