7217
|
1 ## Copyright (C) 2006, 2007 Michel D. Schmid |
|
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 ## Undocumented internal function |
|
20 |
|
21 ## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net> |
|
22 ## Adapted-by: jwe |
|
23 |
|
24 function h = __stem__ (have_z, varargin) |
|
25 |
7218
|
26 if (have_z) |
|
27 caller = "stem3"; |
|
28 else |
|
29 caller = "stem"; |
|
30 endif |
7217
|
31 |
7218
|
32 [ax, varargin, nargin] = __plt_get_axis_arg__ (caller, varargin{:}); |
7217
|
33 |
|
34 [x, y, z, dofill, lc, ls, mc, ms] = check_stem_arg (have_z, varargin{:}); |
|
35 |
|
36 if (dofill) |
|
37 fc = mc; |
|
38 else |
|
39 fc = "none"; |
|
40 endif |
|
41 |
|
42 newplot (); |
|
43 nx = numel (x); |
|
44 xt = x(:)'; |
|
45 xt = [xt; xt; NaN(1, nx)](:); |
|
46 if (have_z) |
|
47 yt = y(:)'; |
|
48 yt = [yt; yt; NaN(1, nx)](:); |
|
49 zt = z(:)'; |
|
50 zt = [zeros(1, nx); zt; NaN(1, nx)](:); |
|
51 else |
|
52 yt = y(:)'; |
|
53 yt = [zeros(1, nx); yt; NaN(1, nx)](:); |
|
54 endif |
|
55 |
|
56 oldax = gca (); |
|
57 unwind_protect |
|
58 axes (ax); |
|
59 |
|
60 if (have_z) |
|
61 h_stems = plot3 (xt, yt, zt, "color", lc, "linestyle", ls, |
|
62 x, y, z, "color", mc, "marker", ms, "linestyle", "", |
|
63 "markerfacecolor", fc); |
|
64 |
|
65 h_baseline = []; |
|
66 else |
|
67 h_stems = plot (xt, yt, "color", lc, "linestyle", ls, |
|
68 x, y, "color", mc, "marker", ms, "linestyle", "", |
|
69 "markerfacecolor", fc); |
|
70 |
|
71 ## Must draw the plot first to get proper x limits. |
|
72 drawnow(); |
|
73 x_axis_range = get (gca, "xlim"); |
|
74 h_baseline = line (x_axis_range, [0, 0], "color", [0, 0, 0]); |
|
75 endif |
|
76 unwind_protect_cleanup |
|
77 axes (oldax); |
|
78 end_unwind_protect |
|
79 |
|
80 h = [h_stems; h_baseline]; |
|
81 |
|
82 endfunction |
|
83 |
|
84 function [x, y, z, dofill, lc, ls, mc, ms] = check_stem_arg (have_z, varargin) |
|
85 |
7218
|
86 if (have_z) |
|
87 caller = "stem3"; |
|
88 else |
|
89 caller = "stem"; |
|
90 endif |
|
91 |
7217
|
92 ## set specifiers to default values |
|
93 [lc, ls, mc, ms] = set_default_values (); |
|
94 dofill = 0; |
|
95 fill_2 = 0; |
|
96 linespec_2 = 0; |
|
97 z = []; |
|
98 |
|
99 ## check input arguments |
|
100 if (nargin == 2) |
|
101 if (have_z) |
|
102 z = varargin{1}; |
7218
|
103 x = 1:rows (z); |
|
104 y = 1:columns (z); |
7217
|
105 else |
|
106 y = varargin{1}; |
|
107 if (isvector (y)) |
7218
|
108 x = 1:length (y); |
7217
|
109 elseif (ismatrix (y)) |
7218
|
110 x = 1:rows (y); |
7217
|
111 else |
|
112 error ("stem: Y must be a matrix"); |
|
113 endif # in each case, x & y will be defined |
|
114 endif |
|
115 elseif (nargin == 3) |
|
116 ## several possibilities |
|
117 ## 1. the real y data |
|
118 ## 2. 'fill' |
|
119 ## 3. line spec |
|
120 if (ischar (varargin{2})) |
|
121 ## only 2. or 3. possible |
|
122 if (strcmp ("fill", varargin{2})) |
|
123 dofill = 1; |
|
124 else |
|
125 ## parse the linespec |
7218
|
126 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{2}); |
7217
|
127 endif |
|
128 if (have_z) |
|
129 z = varargin{1}; |
7218
|
130 x = 1:rows (z); |
|
131 y = 1:columns (z); |
7217
|
132 else |
|
133 y = varargin{1}; |
|
134 if (isvector (y)) |
7218
|
135 x = 1:length (y); |
7217
|
136 elseif (ismatrix (y)) |
7218
|
137 x = 1:rows (y); |
7217
|
138 else |
|
139 error ("stem: Y must be a matrix"); |
|
140 endif # in each case, x & y will be defined |
|
141 endif |
|
142 else |
|
143 if (have_z) |
|
144 error ("stem3: must define X, Y and Z"); |
|
145 else |
|
146 ## must be the real y data |
|
147 x = varargin{1}; |
|
148 y = varargin{2}; |
|
149 if (! (ismatrix (x) && ismatrix (y))) |
|
150 error ("stem: X and Y must be matrices"); |
|
151 endif |
|
152 endif |
|
153 endif |
|
154 elseif (nargin == 4) |
|
155 ## again several possibilities |
|
156 ## arg2 1. real y |
|
157 ## arg2 2. 'fill' or linespec |
|
158 ## arg3 1. real z |
|
159 ## arg3 2. 'fill' or linespec |
|
160 if (ischar (varargin{2})) |
|
161 ## only arg2 2. / arg3 1. & arg3 3. are possible |
|
162 if (strcmp ("fill", varargin{2})) |
|
163 dofill = 1; |
|
164 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
165 else |
|
166 ## must be a linespec |
7218
|
167 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{2}); |
7217
|
168 linespec_2 = 1; |
|
169 endif |
|
170 if (have_z) |
|
171 z = varargin{1}; |
7218
|
172 x = 1:rows (z); |
|
173 y = 1:columns (z); |
7217
|
174 else |
|
175 y = varargin{1}; |
|
176 if (isvector (y)) |
7218
|
177 x = 1:length (y); |
7217
|
178 elseif (ismatrix (y)) |
7218
|
179 x = 1:rows (y); |
7217
|
180 else |
|
181 error ("stem: Y must be a matrix"); |
|
182 endif # in each case, x & y will be defined |
|
183 endif |
|
184 else |
|
185 if (have_z) |
|
186 x = varargin{1}; |
|
187 y = varargin{2}; |
|
188 z = varargin{3}; |
|
189 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z))) |
|
190 error ("stem3: X, Y and Z must be matrices"); |
|
191 endif |
|
192 else |
|
193 ## must be the real y data |
|
194 x = varargin{1}; |
|
195 y = varargin{2}; |
|
196 if (! (ismatrix (x) && ismatrix (y))) |
|
197 error ("stem: X and Y must be matrices"); |
|
198 endif |
|
199 endif |
|
200 endif # if ischar(varargin{2}) |
|
201 if (! have_z) |
7218
|
202 ## varargin{3} must be char |
|
203 ## check for "fill" |
|
204 if (strcmp ("fill", varargin{3}) && fill_2) |
|
205 error ("stem: duplicate fill argument"); |
|
206 elseif (strcmp("fill", varargin{3}) && linespec_2) |
7217
|
207 ## must be "fill" |
|
208 dofill = 1; |
|
209 fill_2 = 1; |
7218
|
210 elseif (strcmp ("fill", varargin{3}) && ! linespec_2) |
7217
|
211 ## must be "fill" |
|
212 dofill = 1; |
|
213 fill_2 = 1; |
|
214 elseif (! linespec_2) |
|
215 ## must be linespec |
7218
|
216 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{3}); |
7217
|
217 linespec_2 = 1; |
|
218 endif |
|
219 endif |
|
220 elseif (nargin == 5) |
|
221 if (have_z) |
|
222 x = varargin{1}; |
|
223 y = varargin{2}; |
|
224 z = varargin{3}; |
|
225 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z))) |
|
226 error ("stem3: X, Y and Z must be matrices"); |
|
227 endif |
|
228 else |
|
229 x = varargin{1}; |
|
230 y = varargin{2}; |
|
231 if (! (ismatrix (x) && ismatrix (y))) |
7218
|
232 error ("stem: X and Y must be matrices"); |
7217
|
233 endif |
|
234 endif |
|
235 |
|
236 if (! have_z) |
|
237 if (strcmp ("fill", varargin{3})) |
|
238 dofill = 1; |
|
239 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
240 else |
|
241 ## must be a linespec |
7218
|
242 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{3}); |
7217
|
243 linespec_2 = 1; |
|
244 endif |
|
245 endif |
|
246 |
|
247 ## check for "fill" .. |
7218
|
248 if (strcmp ("fill", varargin{4}) && fill_2) |
|
249 error ("%s: duplicate fill argument", caller); |
|
250 elseif (strcmp ("fill", varargin{4}) && linespec_2) |
7217
|
251 ## must be "fill" |
|
252 dofill = 1; |
|
253 fill_2 = 1; |
7218
|
254 elseif (! strcmp ("fill", varargin{4}) && ! linespec_2) |
7217
|
255 ## must be linespec |
7218
|
256 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{4}); |
7217
|
257 linespec_2 = 1; |
|
258 endif |
|
259 elseif (nargin == 6 && have_z) |
|
260 x = varargin{1}; |
|
261 y = varargin{2}; |
|
262 z = varargin{3}; |
|
263 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z))) |
|
264 error ("stem3: X, Y and Z must be matrices"); |
|
265 endif |
|
266 |
|
267 if (strcmp ("fill", varargin{4})) |
|
268 dofill = 1; |
|
269 fill_2 = 1; # be sure, no second "fill" is in the arguments |
|
270 else |
|
271 ## must be a linespec |
7218
|
272 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{4}); |
7217
|
273 linespec_2 = 1; |
|
274 endif |
|
275 |
|
276 ## check for "fill" .. |
7218
|
277 if (strcmp ("fill", varargin{5}) && fill_2) |
|
278 error ("stem3: duplicate fill argument"); |
|
279 elseif (strcmp ("fill", varargin{5}) && linespec_2) |
7217
|
280 ## must be "fill" |
|
281 dofill = 1; |
|
282 fill_2 = 1; |
7218
|
283 elseif (! strcmp ("fill", varargin{5}) && ! linespec_2) |
7217
|
284 ## must be linespec |
7218
|
285 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{5}); |
7217
|
286 linespec_2 = 1; |
|
287 endif |
|
288 else |
7218
|
289 error ("%s: incorrect number of arguments", caller); |
7217
|
290 endif |
|
291 |
|
292 endfunction |
|
293 |
7218
|
294 function [lc, ls, mc, ms] = stem_line_spec (caller, str) |
7217
|
295 if (! ischar (str)) |
7218
|
296 error ("%s: expecting argument to be \"fill\" or a string of specifiers", |
|
297 caller); |
7217
|
298 endif |
|
299 [lc, ls, mc, ms] = set_default_values (); |
|
300 ## Parse the line specifier string. |
|
301 cur_props = __pltopt__ ("stem", str, false); |
|
302 for i = 1:length(cur_props) |
|
303 if (isfield (cur_props(i), "color") && ! isempty (cur_props(i).color)); # means line color |
|
304 mc = lc = cur_props(i).color; |
|
305 elseif (isfield (cur_props(i), "linestyle")) |
|
306 ls = cur_props(i).linestyle; |
|
307 elseif (isfield (cur_props(i), "marker") && ! strcmp (cur_props(i).marker, "none")) |
|
308 ms = cur_props(i).marker; |
|
309 endif |
|
310 endfor |
|
311 endfunction |
|
312 |
|
313 function [lc, ls, mc, ms] = set_default_values () |
|
314 ## set default values |
|
315 mc = [1, 0, 0]; |
|
316 lc = [1, 0, 0]; |
|
317 ls = "-"; |
|
318 ms = "o"; |
|
319 endfunction |