2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
1540
|
18 |
3368
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} subplot (@var{rows}, @var{cols}, @var{index}) |
|
21 ## @deftypefnx {Function File} {} subplot (@var{rcn}) |
6448
|
22 ## Set up a plot grid with @var{cols} by @var{rows} subwindows and plot |
|
23 ## in location given by @var{index}. |
3426
|
24 ## |
3368
|
25 ## If only one argument is supplied, then it must be a three digit value |
|
26 ## specifying the location in digits 1 (rows) and 2 (columns) and the plot |
|
27 ## index in digit 3. |
3426
|
28 ## |
3368
|
29 ## The plot index runs row-wise. First all the columns in a row are filled |
|
30 ## and then the next row is filled. |
3426
|
31 ## |
5798
|
32 ## For example, a plot with 2 by 3 grid will have plot indices running as |
2311
|
33 ## follows: |
3368
|
34 ## @iftex |
|
35 ## @tex |
|
36 ## \vskip 10pt |
|
37 ## \hfil\vbox{\offinterlineskip\hrule |
|
38 ## \halign{\vrule#&&\qquad\hfil#\hfil\qquad\vrule\cr |
|
39 ## height13pt&1&2&3&4\cr height12pt&&&&\cr\noalign{\hrule} |
|
40 ## height13pt&5&6&7&8\cr height12pt&&&&\cr\noalign{\hrule}}} |
|
41 ## \hfil |
|
42 ## \vskip 10pt |
|
43 ## @end tex |
|
44 ## @end iftex |
|
45 ## @ifinfo |
|
46 ## @display |
|
47 ## @group |
6257
|
48 ## @example |
|
49 ## |
3368
|
50 ## +-----+-----+-----+-----+ |
|
51 ## | 1 | 2 | 3 | 4 | |
|
52 ## +-----+-----+-----+-----+ |
|
53 ## | 5 | 6 | 7 | 8 | |
|
54 ## +-----+-----+-----+-----+ |
6257
|
55 ## @end example |
3368
|
56 ## @end group |
|
57 ## @end display |
|
58 ## @end ifinfo |
5798
|
59 ## @seealso{plot} |
3368
|
60 ## @end deftypefn |
1540
|
61 |
2312
|
62 ## Author: Vinayak Dutt <Dutt.Vinayak@mayo.EDU> |
|
63 ## Adapted-By: jwe |
1540
|
64 |
6257
|
65 function h = subplot (rows, columns, index) |
6163
|
66 |
1540
|
67 if (nargin != 3 && nargin != 1) |
6046
|
68 print_usage (); |
1540
|
69 endif |
|
70 |
|
71 if (nargin == 1) |
|
72 |
4030
|
73 if (! (isscalar (rows) && rows >= 0)) |
1541
|
74 error ("subplot: input rcn has to be a positive scalar"); |
1540
|
75 endif |
|
76 |
1541
|
77 tmp = rows; |
|
78 index = rem (tmp, 10); |
|
79 tmp = (tmp - index) / 10; |
|
80 columns = rem (tmp, 10); |
|
81 tmp = (tmp - columns) / 10; |
|
82 rows = rem (tmp, 10); |
1540
|
83 |
4030
|
84 elseif (! (isscalar (columns) && isscalar (rows) && isscalar (index))) |
1540
|
85 error ("subplot: columns, rows, and index have to be scalars"); |
|
86 endif |
|
87 |
|
88 columns = round (columns); |
|
89 rows = round (rows); |
|
90 index = round (index); |
|
91 |
|
92 if (index > columns*rows) |
|
93 error ("subplot: index must be less than columns*rows"); |
|
94 endif |
|
95 |
|
96 if (columns < 1 || rows < 1 || index < 1) |
|
97 error ("subplot: columns,rows,index must be be positive"); |
|
98 endif |
|
99 |
6257
|
100 xsize = 1 / columns; |
|
101 ysize = 1 / rows; |
1540
|
102 |
6257
|
103 yp = fix ((index-1)/columns); |
|
104 xp = index - yp*columns - 1; |
1540
|
105 |
6425
|
106 x0 = xp * xsize; |
|
107 y0 = (rows - yp - 1) * ysize; |
1540
|
108 |
6425
|
109 pos = [x0, y0, xsize, ysize]; |
|
110 |
|
111 x1 = x0 + xsize; |
|
112 y1 = y0 + ysize; |
6257
|
113 |
|
114 cf = gcf (); |
1540
|
115 |
6257
|
116 set (cf, "nextplot", "add"); |
1540
|
117 |
6257
|
118 found = false; |
6828
|
119 for child = get (cf, "children") |
6765
|
120 ## Check if this child is still valid; this might not be the case |
|
121 ## anymore due to the deletion of previous children (due to DeleteFcn |
|
122 ## callback or for legends/colorbars that get deleted with their |
|
123 ## corresponding axes) |
|
124 if (! ishandle (child)) |
|
125 continue; |
|
126 endif |
6828
|
127 if (strcmp (get (child, "type"), "axes")) |
|
128 objpos = get (child, "outerposition"); |
6425
|
129 if (objpos == pos) |
|
130 ## If the new axes are in exactly the same position as an |
|
131 ## existing axes object, use the existing axes. |
6257
|
132 found = true; |
|
133 tmp = child; |
|
134 break; |
6425
|
135 else |
|
136 ## If the new axes overlap an old axes object, delete the old |
|
137 ## axes. |
|
138 objx0 = objpos(1); |
|
139 objx1 = objx0 + objpos(3); |
|
140 objy0 = objpos(2); |
|
141 objy1 = objy0 + objpos(4); |
|
142 if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0)) |
|
143 delete (child); |
|
144 endif |
6178
|
145 endif |
1541
|
146 endif |
6257
|
147 endfor |
1540
|
148 |
6424
|
149 if (found) |
|
150 set (cf, "currentaxes", tmp); |
|
151 else |
6257
|
152 tmp = axes ("outerposition", pos); |
|
153 endif |
1540
|
154 |
6257
|
155 if (nargout > 0) |
|
156 h = tmp; |
1540
|
157 endif |
|
158 |
|
159 endfunction |