comparison scripts/plot/stem.m @ 7217:c8abc18322b7

[project @ 2007-11-29 20:50:24 by jwe]
author jwe
date Thu, 29 Nov 2007 20:50:24 +0000
parents e8d953d03f6a
children 02eb1619b857
comparison
equal deleted inserted replaced
7216:5389a52df87b 7217:c8abc18322b7
92 ## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net> 92 ## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net>
93 ## Adapted-by: jwe 93 ## Adapted-by: jwe
94 94
95 function h = stem (varargin) 95 function h = stem (varargin)
96 96
97 if (nargin < 1 || nargin > 4) 97 tmp = __stem__ (false, varargin{:});
98 print_usage ();
99 endif
100
101 [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin{:});
102
103 if (dofill)
104 fc = mc;
105 else
106 fc = "none";
107 endif
108
109 newplot ();
110 nx = numel (x);
111 xt = x(:)';
112 xt = [xt; xt; NaN(1, nx)](:);
113 yt = y(:)';
114 yt = [zeros(1, nx); yt; NaN(1, nx)](:);
115 h_stems = plot (xt, yt, "color", lc, "linestyle", ls,
116 x, y, "color", mc, "marker", ms, "linestyle", "",
117 "markerfacecolor", fc);
118
119 ## Must draw the plot first to get proper x limits.
120 drawnow();
121 x_axis_range = get (gca, "xlim");
122 h_baseline = line (x_axis_range, [0, 0], "color", [0, 0, 0]);
123 98
124 if (nargout > 0) 99 if (nargout > 0)
125 h = [h_stems; h_baseline]; 100 h = tmp;
126 endif 101 endif
127 102
128 endfunction 103 endfunction
129
130 function [x, y, dofill, lc, ls, mc, ms] = check_stem_arg (varargin)
131
132 ## set specifiers to default values
133 [lc, ls, mc, ms] = set_default_values ();
134 dofill = 0;
135 fill_2 = 0;
136 linespec_2 = 0;
137
138 ## check input arguments
139 if (nargin == 1)
140 y = varargin{1};
141 if (isvector (y))
142 x = 1:length(y);
143 elseif (ismatrix (y))
144 x = 1:rows(y);
145 else
146 error ("stem: Y must be a matrix");
147 endif # in each case, x & y will be defined
148
149 elseif (nargin == 2)
150 ## several possibilities
151 ## 1. the real y data
152 ## 2. 'fill'
153 ## 3. line spec
154 if (ischar (varargin{2}))
155 ## only 2. or 3. possible
156 if (strcmp ("fill", varargin{2}))
157 dofill = 1;
158 else
159 ## parse the linespec
160 [lc, ls, mc, ms] = stem_line_spec (varargin{2});
161 endif
162 y = varargin{1};
163 if (isvector (y))
164 x = 1:length(y);
165 elseif (ismatrix (y))
166 x = 1:rows(y);
167 else
168 error ("stem: Y must be a matrix");
169 endif # in each case, x & y will be defined
170 else
171 ## must be the real y data
172 x = varargin{1};
173 y = varargin{2};
174 if (! (ismatrix (x) && ismatrix (y)))
175 error ("stem: X and Y must be matrices");
176 endif
177 endif
178 elseif (nargin == 3)
179 ## again several possibilities
180 ## arg2 1. real y
181 ## arg2 2. 'fill' or linespec
182 ## arg3 1. 'fill' or linespec
183 if (ischar (varargin{2}))
184 ## only arg2 2. / arg3 1. & arg3 3. are possible
185 if (strcmp ("fill", varargin{2}))
186 dofill = 1;
187 fill_2 = 1; # be sure, no second "fill" is in the arguments
188 else
189 ## must be a linespec
190 [lc, ls, mc, ms] = stem_line_spec (varargin{2});
191 linespec_2 = 1;
192 endif
193 y = varargin{1};
194 if (isvector (y))
195 x = 1:length(y);
196 elseif (ismatrix (y))
197 x = 1:size(y,1);
198 else
199 error ("stem: Y must be a matrix");
200 endif # in each case, x & y will be defined
201 else
202 ## must be the real y data
203 x = varargin{1};
204 y = varargin{2};
205 if (! (ismatrix (x) && ismatrix (y)))
206 error ("stem: X and Y must be matrices");
207 endif
208 endif # if ischar(varargin{2})
209 ## varargin{3} must be char...
210 ## check for "fill" ..
211 if (strcmp ("fill", varargin{3}) & fill_2)
212 error ("stem:check_stem_arg: duplicate fill argument");
213 elseif (strcmp("fill", varargin{3}) & linespec_2)
214 # must be "fill"
215 dofill = 1;
216 fill_2 = 1;
217 elseif (strcmp ("fill", varargin{3}) & ! linespec_2)
218 ## must be "fill"
219 dofill = 1;
220 fill_2 = 1;
221 elseif (! linespec_2)
222 ## must be linespec
223 [lc, ls, mc, ms] = stem_line_spec (varargin{3});
224 linespec_2 = 1;
225 endif
226 elseif (nargin == 4)
227 x = varargin{1};
228 y = varargin{2};
229 if (! (ismatrix (x) && ismatrix (y)))
230 error ("X and Y must be matrices");
231 endif
232
233 if (strcmp ("fill", varargin{3}))
234 dofill = 1;
235 fill_2 = 1; # be sure, no second "fill" is in the arguments
236 else
237 ## must be a linespec
238 [lc, ls, mc, ms] = stem_line_spec (varargin{3});
239 linespec_2 = 1;
240 endif
241
242 ## check for "fill" ..
243 if (strcmp ("fill", varargin{4}) & fill_2)
244 error ("stem:check_stem_arg: duplicate fill argument");
245 elseif (strcmp ("fill", varargin{4}) & linespec_2)
246 ## must be "fill"
247 dofill = 1;
248 fill_2 = 1;
249 elseif (! strcmp ("fill", varargin{4}) & ! linespec_2)
250 ## must be linespec
251 [lc, ls, mc, ms] = stem_line_spec (varargin{4});
252 linespec_2 = 1;
253 endif
254 endif
255
256 endfunction
257
258 function [lc, ls, mc, ms] = stem_line_spec (str)
259 if (! ischar (str))
260 error ("stem:stem_line_spec: wrong argument type, must be \"fill\" or a string of specifiers");
261 endif
262 [lc, ls, mc, ms] = set_default_values ();
263 ## Parse the line specifier string.
264 cur_props = __pltopt__ ("stem", str, false);
265 for i = 1:length(cur_props)
266 if (isfield (cur_props(i), "color") && ! isempty (cur_props(i).color)); # means line color
267 mc = lc = cur_props(i).color;
268 elseif (isfield (cur_props(i), "linestyle"))
269 ls = cur_props(i).linestyle;
270 elseif (isfield (cur_props(i), "marker") && ! strcmp (cur_props(i).marker, "none"))
271 ms = cur_props(i).marker;
272 endif
273 endfor
274 endfunction
275
276 function [lc, ls, mc, ms] = set_default_values ()
277 ## set default values
278 mc = [1, 0, 0];
279 lc = [1, 0, 0];
280 ls = "-";
281 ms = "o";
282 endfunction