comparison scripts/plot/copyobj.m @ 14867:97ce18b62d0f

New Functions; copyobj.m, hdl2struct.m, struct2hdl.m * copyobj.m: New File. * hdl2struct.m: New File. * struct2hdl.m: New File. * scripts/plot/module.mk: Add new files. * scripts/help/unimplmented.m: Remove copyobj.m from list.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Mon, 16 Jul 2012 20:37:01 -0400
parents
children 8f0e3c5bfa5f
comparison
equal deleted inserted replaced
14866:2309812f428e 14867:97ce18b62d0f
1 ## Copyright (C) 2012 pdiribarne
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING. If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{hnew} =} copyobj (@var{horig})
19 ## @deftypefnx {Function File} {@var{hnew} =} copyobj (@var{horig}, @var{hparent})
20 ## Constructs a copy of the object associated with handle @var{horig}
21 ## and returns a handle, @var{hnew}, to the new object.
22 ## If a parent handle @var{hparent} (root, figure, axes or hggroup) is specified,
23 ## the copied object will be created as a child to @var{hparent}.
24 ## @seealso{findobj, get, set, struct2hdl, hdl2struct}
25 ## @end deftypefn
26
27 ## Author: pdiribarne <pdiribarne@new-host.home>
28 ## Created: 2012-04-01
29
30 function hout = copyobj (hin, hpar = 0)
31
32 partypes = {"root", "figure", "axes", "hggroup"};
33 othertypes = {"line", "patch", "surface", "image", "text"};
34 alltypes = [partypes othertypes];
35
36 if (! ishandle (hin) || nargin > 2)
37 print_usage ();
38 elseif (! ishandle (hpar))
39 hpar = figure (floor (hpar));
40 elseif (! any (strcmpi (get (hpar).type, partypes)))
41 print_usage ();
42 endif
43
44 ## compatibility of input handles
45 kididx = find (strcmp (alltypes, get (hin).type));
46 paridx = find (strcmp (alltypes, get (hpar).type));
47
48 if (kididx <= paridx)
49 error ("copyobj: %s object can't be children to %s.",
50 alltypes{kididx}, alltypes{paridx})
51 elseif nargin == 1
52 str = hdl2struct (hin);
53 hout = struct2hdl (str);
54 else
55 str = hdl2struct (hin);
56 hout = struct2hdl (str, hpar);
57 endif
58 endfunction
59
60 %!test
61 %! h1 = figure ();
62 %! set (h1, "visible", "off")
63 %! x = 0:0.1:2*pi;
64 %! y1 = sin (x);
65 %! y2 = exp (x - 1);
66 %! ax = plotyy (x,y1, x-1,y2, @plot, @semilogy);
67 %! xlabel ("X");
68 %! ylabel (ax(1), "Axis 1");
69 %! ylabel (ax(2), "Axis 2");
70 %! axes (ax(1));
71 %! text (0.5, 0.5, "Left Axis", ...
72 %! "color", [0 0 1], "horizontalalignment", "center");
73 %! axes (ax(2));
74 %! text (4.5, 80, "Right Axis", ...
75 %! "color", [0 0.5 0], "horizontalalignment", "center");
76 %! s1 = hdl2struct (h1);
77 %! h2 = struct2hdl (s1);
78 %! s2 = hdl2struct (h2);
79 %! png1 = strcat (tmpnam (), ".png");
80 %! png2 = strcat (tmpnam (), ".png");
81 %! unwind_protect
82 %! print (h1, png1)
83 %! [img1, map1, alpha1] = imread (png1);
84 %! print (h2, png2)
85 %! [img2, map2, alpha2] = imread (png2);
86 %! unwind_protect_cleanup
87 %! unlink (png1);
88 %! unlink (png2);
89 %! end_unwind_protect
90 %! assert (img1, img2)
91 %! assert (map1, map2)
92 %! assert (alpha1, alpha2)
93
94 %!demo
95 %! hdl = figure (1234);
96 %! clf ()
97 %! hold on
98 %! x = 1:10;
99 %! y = x.^2;
100 %! dy = 2 * (.2 * x);
101 %! y2 = (x - 3).^2;
102 %! hg = errorbar (x, y, dy,'#~');
103 %! set (hg, 'marker', '^', 'markerfacecolor', rand(1,3))
104 %! plot (x, y2, 'ok-')
105 %! legend ('errorbar', 'line')
106 %! hout = copyobj (1234);
107
108 %!demo
109 %! hdl = figure (1234);
110 %! clf ()
111 %! subplot (2, 2, 1);
112 %! hold on
113 %! [C, H] = contourf (rand(10, 10));
114 %! colorbar
115 %! subplot (2, 2, 2);
116 %! hold on
117 %! quiver (rand(10, 10), rand(10, 10))
118 %! subplot (2, 2, 3);
119 %! colormap (jet (64))
120 %! sombrero;
121 %! colorbar('peer', gca, 'NorthOutside')
122 %! subplot (2, 2, 4);
123 %! imagesc (rand (30, 30));
124 %! text (15, 15, 'Rotated text', ...
125 %! 'HorizontAlalignment', 'Center', 'Rotation', 30);
126 %! hout = copyobj (1234);
127