annotate scripts/time/datetick.m @ 9934:519de8bb56ab

Add CLEANFILES and MAINTAINERCLEANFILES objects to properly clean refcard directory
author Rik <rdrider0-list@yahoo.com>
date Mon, 07 Dec 2009 14:04:56 -0800
parents 16f53d29049f
children c2f1cdb59821
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9245
16f53d29049f update copyright notices
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
1 ## Copyright (C) 2008, 2009 David Bateman
8112
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 ## @deftypefn {Function File} {} datetick (@var{form})
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
21 ## @deftypefnx {Function File} {} datetick (@var{axis}, @var{form})
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
22 ## @deftypefnx {Function File} {} datetick (@dots{}, "keeplimits")
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
23 ## @deftypefnx {Function File} {} datetick (@dots{}, "keepticks")
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
24 ## @deftypefnx {Function File} {} datetick (@dots{ax}, @dots{})
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8325
diff changeset
25 ## Adds date formatted tick labels to an axis. The axis the apply the
8112
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
26 ## ticks to is determined by @var{axis} that can take the values "x",
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8325
diff changeset
27 ## "y" or "z". The default value is "x". The formatting of the labels is
8112
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 ## determined by the variable @var{form}, that can either be a string in
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 ## the format needed by @code{dateform}, or a positive integer that can
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 ## be accepted by @code{datestr}.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 ## @seealso{datenum, datestr}
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 ## @end deftypefn
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
33
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 function datetick (varargin)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:});
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 if (nargin < 1)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 print_usage ();
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 oldh = gca ();
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 unwind_protect
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 axes (h);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 __datetick__ (varargin{:});
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 unwind_protect_cleanup
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 axes (oldh);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 end_unwind_protect
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
49 endfunction
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
50
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
51 %!demo
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 %! yr = 1900:10:2000;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 %! pop = [76.094, 92.407, 106.461, 123.077 131.954, 151.868, 179.979, ...
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 %! 203.984, 227.225, 249.623, 282.224];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 %! plot (datenum (yr, 1, 1), pop);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 %! title ("US population (millions)");
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 %! xlabel ("Year");
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 %! datetick ("x", "YYYY");
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
59
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 function __datetick__ (varargin)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
61
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
62 keeplimits = false;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
63 keeptick = false;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
64 idx = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 for i = 1 : nargin
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 arg = varargin {i};
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 if (ischar (arg))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 if (strcmpi (arg, "keeplimits"))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 keeplimits = true;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
70 idx = [idx, i];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
71 elseif (strcmpi (arg, "keeptick"))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 keeptick = true;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
73 idx = [idx, i];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
76 endfor
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
77
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
78 varargin(idx) = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 nargin = length (varargin);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
80 form = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 ax = "x";
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
82
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 if (nargin != 0)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
84 arg = varargin{1};
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
85 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") ||
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
86 strcmp (arg, "z")))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
87 ax = arg;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
88 if (nargin > 1)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
89 form = varargin{2};
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
90 varargin(1:2) = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
91 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 varargin(1) = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
93 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
94 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
95 form = arg;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
96 varargin(1) = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
97 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
98 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
99
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
100 ## Don't publish the existence of this variable for use with dateaxis
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
101 if (length (varargin) > 0)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
102 startdate = varargin{1};
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
103 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
104 startdate = [];
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
105 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
106
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
107 if (isnumeric (form))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
108 if (! isscalar (form) || floor (form) != form || form < 0)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
109 error ("datetick: expecting form argument to be a positive integer");
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
110 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
111 elseif (! ischar (form) && !isempty (form))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
112 error ("datetick: expecting valid date format string");
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
113 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
114
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
115 if (keeptick)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
116 ticks = get (gca (), strcat (ax, "tick"))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
117 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
118 ## Need to do our own axis tick position calculation as
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
119 ## year, etc, don't fallback on nice datenum values.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
120 objs = findall (gca());
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
121 xmax = NaN;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
122 xmin = NaN;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
123 for i = 1 : length (objs)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
124 fld = get (objs (i));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
125 if (isfield (fld, strcat (ax, "data")))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
126 xdata = getfield (fld, strcat (ax, "data"))(:);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
127 xmin = min (xmin, min (xdata));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
128 xmax = max (xmax, max (xdata));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
129 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
130 endfor
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
131
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
132 if (isnan (xmin) || isnan (xmax))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
133 xmin = 0;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
134 xmax = 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
135 elseif (xmin == xmax)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
136 xmax = xmin + 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
137 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
138
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
139 N = 3;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
140 if (xmax - xmin < N)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
141 ## Day scale or less
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
142 if (xmax - xmin < N / 24 / 60 / 60)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
143 scl = 1 / 24 / 60 / 60;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
144 elseif (xmax - xmin < N / 24 / 6)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
145 scl = 1 / 24 / 60;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
146 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
147 scl = 1 / 24;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
148 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
149 sep = __calc_tick_sep__ (xmin / scl , xmax / scl);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
150 xmin = sep * floor (xmin / scl / sep);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
151 xmax = sep * ceil (xmax / scl / sep);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
152 nticks = (xmax - xmin) / sep + 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
153 xmin *= scl;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
154 xmax *= scl;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
155 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
156 [ymin, mmin, dmin] = datevec (xmin);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
157 [ymax, mmax, dmax] = datevec (xmax);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
158 minyear = ymin + (mmin - 1) / 12 + (dmin - 1) / 12 / 30;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
159 maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
160 minmonth = mmin + (dmin - 1) / 30;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
161 maxmonth = (ymax - ymin) * 12 + mmax + (dmax - 1) / 30;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
162
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
163 if (maxmonth - minmonth < N)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
164 sep = __calc_tick_sep__ (xmin, xmax);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
165 xmin = sep * floor (xmin / sep);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
166 xmax = sep * ceil (xmax / sep);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
167 nticks = (xmax - xmin) / sep + 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
168 elseif (maxyear - minyear < N)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
169 sep = __calc_tick_sep__ (minmonth , maxmonth);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
170 xmin = datenum (ymin, sep * floor (minmonth / sep), 1);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
171 xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
172 nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
173 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
174 sep = __calc_tick_sep__ (minyear , maxyear);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
175 xmin = datenum (sep * floor (minyear / sep), 1, 1);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
176 xmax = datenum (sep * ceil (maxyear / sep), 1, 1);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
177 nticks = ceil (maxyear / sep) - floor (minyear / sep) + 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
178 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
179 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
180 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
181 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
182
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
183 if (isempty (form))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
184 r = max(ticks) - min(ticks);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
185 if r < 10/60/24
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
186 ## minutes and seconds
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
187 form = 13;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
188 elseif r < 2
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
189 ## hours
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
190 form = 15;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
191 elseif r < 15
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
192 ## days
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
193 form = 8;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
194 elseif r < 365
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
195 ## months
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
196 form = 6;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
197 elseif r < 90*12
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
198 ## quarters
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
199 form = 27;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
200 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
201 ## years
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
202 form = 10;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
203 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
204 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
205
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
206 if (length (ticks) == 6)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
207 ## Careful that its not treated as a datevec
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
208 if (! isempty (startdate))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
209 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form),
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
210 datestr (ticks(end) - ticks(1) + startdate, form));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
211 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
212 sticks = strvcat (datestr (ticks(1:end-1), form),
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
213 datestr (ticks(end), form));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
214 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
215 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
216 if (! isempty (startdate))
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
217 sticks = datestr (ticks - ticks(1) + startdate, form);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
218 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
219 sticks = datestr (ticks, form);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
220 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
221 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
222
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
223 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
224
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
225 if (keeptick)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
226 if (keeplimits)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
227 set (gca(), strcat (ax, "ticklabel"), sticks);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
228 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
229 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"),
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
230 [min(ticks), max(ticks)]);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
231 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
232 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
233 if (keeplimits)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
234 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
235 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
236 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks,
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
237 strcat (ax, "lim"), [min(ticks), max(ticks)]);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
238 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
239 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
240 endfunction
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
241
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
242 function [a, b] = __magform__ (x)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
243 if (x == 0)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
244 a = 0;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
245 b = 0;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
246 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
247 l = log10 (abs (x));
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
248 r = fmod (l, 1);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
249 a = 10 .^ r;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
250 b = fix (l - r);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
251 if (a < 1)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
252 a *= 10;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
253 b -= 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
254 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
255 if (x < 0)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
256 a = -a;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
257 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
258 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
259 endfunction
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
260
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
261 ## A translation from Tom Holoryd's python code at
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
262 ## http://kurage.nimh.nih.gov/tomh/tics.py
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
263 function sep = __calc_tick_sep__ (lo, hi)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
264 persistent sqrt_2 = sqrt (2.0);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
265 persistent sqrt_10 = sqrt (10.0);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
266 persistent sqrt_50 = sqrt (50.0);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
267
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
268 ticint = 5;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
269
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
270 ## Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
271 ## SCALE3 for Determination of Scales on Computer Generated
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
272 ## Plots", Communications of the ACM, 10 (1973), 639-640.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
273 ## Also cited as ACM Algorithm 463.
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
274
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
275 [a, b] = __magform__ ((hi - lo) / ticint);
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
276
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
277 if (a < sqrt_2)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
278 x = 1;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
279 elseif (a < sqrt_10)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
280 x = 2;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
281 elseif (a < sqrt_50)
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
282 x = 5;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
283 else
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
284 x = 10;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
285 endif
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
286 sep = x * 10 .^ b;
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
287 endfunction
31e86163b752 Add the datetick function
David Bateman <dbateman@free.fr>
parents:
diff changeset
288