Mercurial > hg > octave-lyh
changeset 17218:73dd413f2c3e
datetick.m: Use modern coding conventions in function.
* scripts/time/datetick.m: Use modern coding conventions in function.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 08 Aug 2013 14:34:12 -0700 |
parents | eb88f77b3e26 |
children | 87ba70043bfc |
files | scripts/time/datetick.m |
diffstat | 1 files changed, 38 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/time/datetick.m +++ b/scripts/time/datetick.m @@ -33,14 +33,23 @@ function datetick (varargin) - [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:}); + [hax, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:}); - oldh = gca (); + oldfig = ifelse (isempty (hax), [], get (0, "currentfigure")); + if (isempty (hax)) + hax = gca (); + endif + unwind_protect - axes (h); + ## FIXME: This will bring the axes to the top of the stack. + ## This may not always be desirable if there are multiple axes + ## objects. + axes (hax); __datetick__ (varargin{:}); unwind_protect_cleanup - axes (oldh); + if (! isempty (oldfig)) + set (0, "currentfigure", oldfig); + endif end_unwind_protect endfunction @@ -61,7 +70,7 @@ %! yr = 1988:2:2002; %! yr = datenum (yr,1,1); %! pr = [12.1 13.3 12.6 13.1 13.3 14.1 14.4 15.2]; -%! plot (yr, pr); +%! plot (yr, pr, "-o"); %! xlabel ("year"); %! ylabel ("average price"); %! ax = gca; @@ -75,31 +84,26 @@ function __datetick__ (varargin) keeplimits = false; + idx = strcmpi (varargin, "keeplimits"); + if (any (idx)) + keeplimits = true; + varargin = varargin(! idx); + endif keepticks = false; - idx = []; - for i = 1 : nargin - arg = varargin {i}; - if (ischar (arg)) - if (strcmpi (arg, "keeplimits")) - keeplimits = true; - idx = [idx, i]; - elseif (strcmpi (arg, "keepticks")) - keepticks = true; - idx = [idx, i]; - endif - endif - endfor + idx = strcmpi (varargin, "keepticks"); + if (any (idx)) + keepticks = true; + varargin = varargin(! idx); + endif - varargin(idx) = []; - nargin = length (varargin); + nargin = numel (varargin); form = []; ax = "x"; if (nargin != 0) arg = varargin{1}; - if (ischar (arg) && (strcmp (arg, "x") || strcmp (arg, "y") - || strcmp (arg, "z"))) - ax = arg; + if (ischar (arg) && any (strcmpi (arg, {"x", "y", "z"}))) + ax = tolower (arg); if (nargin > 1) form = varargin{2}; varargin(1:2) = []; @@ -121,7 +125,7 @@ if (! isempty (form)) if (isnumeric (form)) - if (! isscalar (form) || floor (form) != form || form < 0) + if (! isscalar (form) || form < 0 || form != fix (form)) error ("datetick: expecting FORM argument to be a positive integer"); endif elseif (! ischar (form)) @@ -130,7 +134,7 @@ endif if (keepticks) - ticks = get (gca (), strcat (ax, "tick")); + ticks = get (gca (), [ax "tick"]); else ## Need to do our own axis tick position calculation as ## year, etc, don't fallback on nice datenum values. @@ -139,8 +143,8 @@ xmin = NaN; for i = 1 : length (objs) fld = get (objs (i)); - if (isfield (fld, strcat (ax, "data"))) - xdata = getfield (fld, strcat (ax, "data"))(:); + if (isfield (fld, [ax "data"])) + xdata = getfield (fld, [ax "data"])(:); xmin = min (xmin, min (xdata)); xmax = max (xmax, max (xdata)); endif @@ -209,7 +213,7 @@ ## days form = 8; elseif (r < 365) - ## FIXME -- FORM should be 19 for European users who use dd/mm + ## FIXME: FORM should be 19 for European users who use dd/mm ## instead of mm/dd. How can that be determined automatically? ## months form = 6; @@ -243,17 +247,17 @@ if (keepticks) if (keeplimits) - set (gca (), strcat (ax, "ticklabel"), sticks); + set (gca (), [ax "ticklabel"], sticks); else - set (gca (), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"), - [min(ticks), max(ticks)]); + set (gca (), [ax "ticklabel"], sticks, + [ax "lim"], [min(ticks), max(ticks)]); endif else if (keeplimits) - set (gca (), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks); + set (gca (), [ax "tick"], ticks, [ax "ticklabel"], sticks); else - set (gca (), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks, - strcat (ax, "lim"), [min(ticks), max(ticks)]); + set (gca (), [ax "tick"], ticks, [ax "ticklabel"], sticks, + [ax "lim"], [min(ticks), max(ticks)]); endif endif endfunction