7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004, |
|
2 ## 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
245
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} stairs (@var{x}, @var{y}) |
6895
|
22 ## Produce a stairstep plot. The arguments may be vectors or matrices. |
3426
|
23 ## |
2311
|
24 ## If only one argument is given, it is taken as a vector of y-values |
3368
|
25 ## and the x coordinates are taken to be the indices of the elements. |
3426
|
26 ## |
2311
|
27 ## If two output arguments are specified, the data are generated but |
|
28 ## not plotted. For example, |
3426
|
29 ## |
3368
|
30 ## @example |
|
31 ## stairs (x, y); |
|
32 ## @end example |
3426
|
33 ## |
3368
|
34 ## @noindent |
2311
|
35 ## and |
3426
|
36 ## |
3368
|
37 ## @example |
6895
|
38 ## @group |
3368
|
39 ## [xs, ys] = stairs (x, y); |
|
40 ## plot (xs, ys); |
6895
|
41 ## @end group |
3368
|
42 ## @end example |
3426
|
43 ## |
3368
|
44 ## @noindent |
2311
|
45 ## are equivalent. |
5642
|
46 ## @seealso{plot, semilogx, semilogy, loglog, polar, mesh, contour, |
6448
|
47 ## bar, xlabel, ylabel, title} |
3368
|
48 ## @end deftypefn |
4
|
49 |
2314
|
50 ## Author: jwe |
|
51 |
2311
|
52 function [xs, ys] = stairs (x, y) |
4
|
53 |
6257
|
54 if (nargin < 1 || nargin > 2) |
|
55 print_usage (); |
|
56 endif |
4
|
57 |
|
58 if (nargin == 1) |
6257
|
59 if (ismatrix (x)) |
|
60 if (isvector (x)) |
|
61 x = x(:); |
|
62 endif |
|
63 y = x; |
|
64 x = 1:rows (y); |
4
|
65 endif |
6257
|
66 endif |
|
67 |
|
68 if (ndims (x) > 2 || ndims (y) > 2) |
|
69 error ("stairs: expecting 2-d arguments"); |
|
70 endif |
|
71 |
|
72 vec_x = isvector (x); |
|
73 |
|
74 if (vec_x) |
|
75 x = x(:); |
|
76 endif |
|
77 |
|
78 if (isvector (y)) |
|
79 y = y(:); |
|
80 endif |
|
81 |
|
82 if (ismatrix (y)) |
|
83 [nr, nc] = size (y); |
|
84 if (vec_x) |
|
85 x = repmat (x, [1, nc]); |
|
86 else |
|
87 [x_nr, x_nc] = size (x); |
|
88 if (x_nr != nr || x_nc != nc) |
|
89 error ("stairs: argument size mismatch"); |
4
|
90 endif |
|
91 endif |
|
92 endif |
|
93 |
6257
|
94 len = 2*nr - 1; |
|
95 |
|
96 tmp_xs = tmp_ys = zeros (len, nc); |
|
97 |
|
98 tmp_xs(1,:) = x(1,:); |
|
99 tmp_ys(1,:) = y(1,:); |
|
100 |
|
101 tmp_x = x(2:nr,:); |
|
102 ridx = 2:2:len-1; |
|
103 tmp_xs(ridx,:) = tmp_x; |
|
104 tmp_ys(ridx,:) = y(1:nr-1,:); |
|
105 |
|
106 ridx = 3:2:len; |
|
107 tmp_xs(ridx,:) = tmp_x; |
|
108 tmp_ys(ridx,:) = y(2:nr,:); |
|
109 |
736
|
110 if (nargout == 0) |
|
111 plot (tmp_xs, tmp_ys); |
|
112 else |
|
113 xs = tmp_xs; |
|
114 ys = tmp_ys; |
4
|
115 endif |
|
116 |
|
117 endfunction |
7245
|
118 |
|
119 %!demo |
|
120 %! x = 1:10; |
|
121 %! y = rand (1, 10); |
|
122 ## stairs (x, y); |
|
123 |
|
124 %!demo |
|
125 %! x = 1:10; |
|
126 %! y = rand (1, 10); |
|
127 %! [xs, ys] = stairs (x, y); |
|
128 %! plot (xs, ys); |