]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/grid.m
update packages
[CreaPhase.git] / octave_packages / m / plot / grid.m
1 ## Copyright (C) 1993-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} grid (@var{arg})
21 ## @deftypefnx {Function File} {} grid ("minor", @var{arg2})
22 ## @deftypefnx {Function File} {} grid (@var{hax}, @dots{})
23 ## Force the display of a grid on the plot.
24 ## The argument may be either @code{"on"}, or @code{"off"}.
25 ## If it is omitted, the current grid state is toggled.
26 ##
27 ## If @var{arg} is @code{"minor"} then the minor grid is toggled.  When
28 ## using a minor grid a second argument @var{arg2} is allowed, which can
29 ## be either @code{"on"} or @code{"off"} to explicitly set the state of
30 ## the minor grid.
31 ##
32 ## If the first argument is an axis handle, @var{hax}, operate on the
33 ## specified axis object.
34 ## @seealso{plot}
35 ## @end deftypefn
36
37 ## Author: jwe
38
39 function grid (varargin)
40
41   [ax, varargin, nargs] = __plt_get_axis_arg__ ("grid", varargin{:});
42
43   grid_on = (strcmp (get (ax, "xgrid"), "on")
44              && strcmp (get (ax, "ygrid"), "on")
45              && strcmp (get (ax, "zgrid"), "on"));
46
47   minor_on = (strcmp (get (ax, "xminorgrid"), "on")
48               && strcmp (get (ax, "yminorgrid"), "on")
49               && strcmp (get (ax, "zminorgrid"), "on"));
50
51   if (nargs > 2)
52     print_usage ();
53   elseif (nargs == 0)
54     grid_on = ! grid_on;
55   else
56     x = varargin{1};
57     if (ischar (x))
58       if (strcmpi (x, "off"))
59         grid_on = false;
60       elseif (strcmpi (x, "on"))
61         grid_on = true;
62       elseif (strcmpi (x, "minor"))
63         if (nargs == 2)
64           x2 = varargin{2};
65           if (strcmpi (x2, "on"))
66             minor_on = true;
67             grid_on = true;
68           elseif (strcmpi (x2, "off"))
69             minor_on = false;
70           else
71             print_usage ();
72           endif
73         else
74            minor_on = ! minor_on;
75            if (minor_on)
76              grid_on = true;
77            endif
78         endif
79       else
80         print_usage ();
81       endif
82     else
83       error ("grid: argument must be a string");
84     endif
85   endif
86
87   if (grid_on)
88     set (ax, "xgrid", "on", "ygrid", "on", "zgrid", "on");
89     if (minor_on)
90       set (ax, "xminorgrid", "on", "yminorgrid", "on", "zminorgrid", "on");
91     else
92       set (ax, "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off");
93     endif
94   else
95     set (ax, "xgrid", "off", "ygrid", "off", "zgrid", "off");
96     set (ax, "xminorgrid", "off", "yminorgrid", "off", "zminorgrid", "off");
97   endif
98
99 endfunction
100
101 %!demo
102 %! clf
103 %! subplot (2,2,1)
104 %! plot (1:100)
105 %! grid minor
106 %! grid minor
107 %! grid
108 %! title ("no grid")
109 %! subplot (2,2,2)
110 %! plot (1:100)
111 %! grid
112 %! title ("grid on")
113 %! subplot (2,2,3)
114 %! plot (1:100)
115 %! grid minor
116 %! title ("grid minor")
117 %! subplot (2,2,4)
118 %! semilogy (1:100)
119 %! grid minor
120 %! title ("grid minor")
121