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