comparison scripts/plot/hold.m @ 17049:0322e057697f

hold.m, grid.m, box.m: Update to use new __plt_get_axis_arg__. * scripts/plot/box.m: Update to use new __plt_get_axis_arg__. Use hax instead of ax. Redo docstring. * scripts/plot/grid.m: Update to use new __plt_get_axis_arg__. Use hax instead of ax. Redo docstring. Move input validation to front of function. * scripts/plot/hold.m: Update to use new __plt_get_axis_arg__. Use hax instead of ax. Redo docstring. Add 2 new %!demos.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Tue, 23 Jul 2013 15:10:57 +0200
parents f3d52523cde1
children eaab03308c0b
comparison
equal deleted inserted replaced
17048:3f99d7d22bd0 17049:0322e057697f
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Command} {} hold 20 ## @deftypefn {Command} {} hold
21 ## @deftypefnx {Command} {} hold @var{state} 21 ## @deftypefnx {Command} {} hold on
22 ## @deftypefnx {Command} {} hold off
23 ## @deftypefnx {Command} {} hold all
22 ## @deftypefnx {Function File} {} hold (@var{hax}, @dots{}) 24 ## @deftypefnx {Function File} {} hold (@var{hax}, @dots{})
23 ## Toggle or set the "hold" state of the plotting engine which determines 25 ## Toggle or set the "hold" state of the plotting engine which determines
24 ## whether new graphic objects are added to the plot or replace the existing 26 ## whether new graphic objects are added to the plot or replace the existing
25 ## objects. 27 ## objects.
26 ## 28 ##
28 ## @item hold on 30 ## @item hold on
29 ## Retain plot data and settings so that subsequent plot commands are displayed 31 ## Retain plot data and settings so that subsequent plot commands are displayed
30 ## on a single graph. 32 ## on a single graph.
31 ## 33 ##
32 ## @item hold all 34 ## @item hold all
33 ## Retain plot line color, line style, data and settings so that subsequent 35 ## Retain plot line color, line style, data, and settings so that subsequent
34 ## plot commands are displayed on a single graph with the next line color and 36 ## plot commands are displayed on a single graph with the next line color and
35 ## style. 37 ## style.
36 ## 38 ##
37 ## @item hold off 39 ## @item hold off
38 ## Clear plot and restore default graphics settings before each new plot 40 ## Restore default graphics settings which clear the graph and reset axis
39 ## command. (default). 41 ## properties before each new plot command. (default).
40 ## 42 ##
41 ## @item hold 43 ## @item hold
42 ## Toggle the current hold state. 44 ## Toggle the current hold state.
43 ## @end table 45 ## @end table
44 ## 46 ##
45 ## When given the additional argument @var{hax}, the hold state is modified 47 ## When given the additional argument @var{hax}, the hold state is modified
46 ## only for the given axis handle. 48 ## only for the given axis handle.
47 ## 49 ##
48 ## To query the current hold state use the @code{ishold} function. 50 ## To query the current hold state use the @code{ishold} function.
49 ## @seealso{ishold, cla, newplot, clf} 51 ## @seealso{ishold, cla, clf, newplot}
50 ## @end deftypefn 52 ## @end deftypefn
51 53
52 function hold (varargin) 54 function hold (varargin)
53 55
54 if (nargin > 0 && numel (varargin{1}) == 1 && ishandle (varargin{1}) 56 if (nargin > 0 && isscalar (varargin{1}) && ishandle (varargin{1})
55 && strcmp (get (varargin{1}, "type"), "axes")) 57 && strcmp (get (varargin{1}, "type"), "axes"))
56 [ax, varargin, nargs] = __plt_get_axis_arg__ ("hold", varargin{:}); 58 [hax, varargin, nargs] = __plt_get_axis_arg__ ("hold", varargin{:});
57 fig = get (ax, "parent"); 59 if (isempty (hax))
60 hax = gca ();
61 endif
62 ## FIXME: Should this be ancestor (hax, "parent")?
63 hfig = get (hax, "parent");
58 elseif (nargin > 0 && numel (varargin{1}) > 1 && ishandle (varargin{1})) 64 elseif (nargin > 0 && numel (varargin{1}) > 1 && ishandle (varargin{1}))
59 print_usage (); 65 print_usage ();
60 else 66 else
61 ax = gca (); 67 hax = gca ();
62 fig = gcf (); 68 hfig = gcf ();
63 nargs = numel (varargin); 69 nargs = numel (varargin);
64 endif 70 endif
65 71
66 hold_all = false; 72 hold_all = false;
67 if (nargs == 0) 73 if (nargs == 0)
68 turn_hold_off = ishold (ax); 74 turn_hold_off = ishold (hax);
69 elseif (nargs == 1) 75 elseif (nargs == 1)
70 state = varargin{1}; 76 state = varargin{1};
71 if (ischar (state)) 77 if (ischar (state))
72 if (strcmpi (state, "off")) 78 if (strcmpi (state, "off"))
73 turn_hold_off = true; 79 turn_hold_off = true;
83 else 89 else
84 print_usage (); 90 print_usage ();
85 endif 91 endif
86 92
87 if (turn_hold_off) 93 if (turn_hold_off)
88 set (ax, "nextplot", "replace"); 94 set (hax, "nextplot", "replace");
89 else 95 else
90 set (ax, "nextplot", "add"); 96 set (hax, "nextplot", "add");
91 set (fig, "nextplot", "add"); 97 set (hfig, "nextplot", "add");
92 endif 98 endif
93 set (ax, "__hold_all__", hold_all); 99 set (hax, "__hold_all__", hold_all);
94 100
95 endfunction 101 endfunction
96 102
103
104 %!demo
105 %! clf;
106 %! t = linspace (0, 2*pi, 100);
107 %! plot (t, sin (t));
108 %! hold on;
109 %! plot (t, cos (t));
110 %! title ({'hold on', '2 plots shown on same graph'});
111 %! hold off;
112
113 %!demo
114 %! clf;
115 %! t = linspace (0, 2*pi, 100);
116 %! plot (t, sin (t));
117 %! hold all;
118 %! plot (t, cos (t));
119 %! title ({'hold all', '2 plots shown on same graph with linestyle also preserved'});
120 %! hold off;
97 121
98 %!demo 122 %!demo
99 %! clf; 123 %! clf;
100 %! A = rand (100); 124 %! A = rand (100);
101 %! [X, Y] = find (A > 0.9); 125 %! [X, Y] = find (A > 0.9);
141 %! xlim ([-2.0 2.0]); 165 %! xlim ([-2.0 2.0]);
142 %! ylim ([-2.0 2.0]); 166 %! ylim ([-2.0 2.0]);
143 %! colorbar ('SouthOutside'); 167 %! colorbar ('SouthOutside');
144 %! title ('Test script for some plot functions'); 168 %! title ('Test script for some plot functions');
145 169
146 ##hold on 170 ## hold on test
147 %!test 171 %!test
148 %! hf = figure ("visible", "off"); 172 %! hf = figure ("visible", "off");
149 %! unwind_protect 173 %! unwind_protect
150 %! p = plot ([0 1]); 174 %! p = plot ([0 1]);
151 %! assert (! ishold); 175 %! assert (! ishold);
157 %! assert (length (get (gca, "children")), 3); 181 %! assert (length (get (gca, "children")), 3);
158 %! unwind_protect_cleanup 182 %! unwind_protect_cleanup
159 %! close (hf); 183 %! close (hf);
160 %! end_unwind_protect 184 %! end_unwind_protect
161 185
162 ##hold off 186 ## hold off test
163 %!test 187 %!test
164 %! hf = figure ("visible", "off"); 188 %! hf = figure ("visible", "off");
165 %! unwind_protect 189 %! unwind_protect
166 %! p = plot ([0 1]); 190 %! p = plot ([0 1]);
167 %! assert (! ishold); 191 %! assert (! ishold);