comparison scripts/plot/util/copyobj.m @ 21626:1dda942a2514

copyobj.m: Allow input vectors for horig or hparent (bug #47694). * copyobj.m: Rewrite doctring to mention new capability. Use for loops to iterate over multiple handles in horig or hparent when present.
author Rik <rik@octave.org>
date Thu, 14 Apr 2016 12:51:43 -0700
parents ecce63c99c3f
children 2f9078956a63
comparison
equal deleted inserted replaced
21625:3cadca91e390 21626:1dda942a2514
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {} {@var{hnew} =} copyobj (@var{horig}) 20 ## @deftypefn {} {@var{hnew} =} copyobj (@var{horig})
21 ## @deftypefnx {} {@var{hnew} =} copyobj (@var{horig}, @var{hparent}) 21 ## @deftypefnx {} {@var{hnew} =} copyobj (@var{horig}, @var{hparent})
22 ## Construct a copy of the graphic object associated with handle @var{horig} 22 ## Construct a copy of the graphic objects associated with the handles
23 ## and return a handle @var{hnew} to the new object. 23 ## @var{horig} and return new handles @var{hnew} to the new objects.
24 ## 24 ##
25 ## If a parent handle @var{hparent} (root, figure, axes, or hggroup) is 25 ## If a parent handle @var{hparent} (root, figure, axes, or hggroup) is
26 ## specified, the copied object will be created as a child of @var{hparent}. 26 ## specified, the copied object will be created as a child of @var{hparent}.
27 ##
28 ## If @var{horig} is a vector of handles, and @var{hparent} is a scalar,
29 ## then each handle in the vector @var{hnew} has its @qcode{"Parent"} property
30 ## set to @var{hparent}. Conversely, if @var{horig} is a scalar and
31 ## @var{hparent} a vector, then each parent object will receive a copy of
32 ## @var{horig}. If @var{horig} and @var{hparent} are both vectors with the
33 ## same number of elements then @code{@var{hnew}(i)} will have parent
34 ## @code{@var{hparent}(i)}.
27 ## @seealso{struct2hdl, hdl2struct, findobj} 35 ## @seealso{struct2hdl, hdl2struct, findobj}
28 ## @end deftypefn 36 ## @end deftypefn
29 37
30 ## Author: pdiribarne <pdiribarne@new-host.home> 38 ## Author: pdiribarne <pdiribarne@new-host.home>
31 ## Created: 2012-04-01 39 ## Created: 2012-04-01
38 46
39 if (! ishandle (horig) || nargin > 2) 47 if (! ishandle (horig) || nargin > 2)
40 print_usage (); 48 print_usage ();
41 elseif (! ishandle (hparent)) 49 elseif (! ishandle (hparent))
42 hparent = figure (fix (hparent)); 50 hparent = figure (fix (hparent));
43 elseif (! any (strcmpi (get (hparent).type, partypes))) 51 else
44 print_usage (); 52 for hp = hparent(:)'
53 if (! any (strcmpi (get (hp, "type"), partypes)))
54 print_usage ();
55 endif
56 endfor
57 endif
58
59 if (numel (horig) != numel (hparent)
60 && ! (isscalar (hparent) || isscalar (horig)))
61 error ("copyobj: size of HORIG and HPARENT must match, or one must be a scalar");
45 endif 62 endif
46 63
47 ## current figure and axes 64 ## current figure and axes
48 cf = gcf (); 65 cf = gcf ();
49 ca = get (cf, "currentaxes"); 66 ca = get (cf, "currentaxes");
50 67
51 ## compatibility of input handles 68 ## compatibility of input handles
52 kididx = find (strcmp (alltypes, get (horig).type)); 69 for i = 1:numel (horig)
53 paridx = find (strcmp (alltypes, get (hparent).type)); 70 kididx(i) = find (strcmp (alltypes, get (horig(i), "type")));
71 endfor
72
73 for i = 1:numel (hparent)
74 paridx(i) = find (strcmp (alltypes, get (hparent(i), "type")));
75 endfor
54 76
55 if (kididx <= paridx) 77 if (kididx <= paridx)
56 error ("copyobj: %s object can't be children to %s.", 78 error ("copyobj: %s object can't be a child of %s.",
57 alltypes{kididx}, alltypes{paridx}); 79 alltypes{kididx}, alltypes{paridx});
58 elseif (nargin == 1) 80 endif
81
82 ## loop over vector inputs
83 if (nargin == 1)
84 ## No parent specified
85 for i = numel (horig)
86 str = hdl2struct (horig(i));
87 hnew(i) = struct2hdl (str);
88 endfor
89 elseif (isscalar (hparent))
90 for i = 1:numel (horig)
91 str = hdl2struct (horig(i));
92 hnew(i) = struct2hdl (str, hparent);
93 endfor
94 elseif (isscalar (horig))
59 str = hdl2struct (horig); 95 str = hdl2struct (horig);
60 hnew = struct2hdl (str); 96 for i = 1:numel (hparent)
97 hnew(i) = struct2hdl (str, hparent(i));
98 endfor
61 else 99 else
62 str = hdl2struct (horig); 100 for i = 1:numel (horig)
63 hnew = struct2hdl (str, hparent); 101 str = hdl2struct (horig(i));
102 hnew(i) = struct2hdl (str, hparent(i));
103 endfor
64 endif 104 endif
65 105
66 ## reset current figure (and eventually axes) to original 106 ## reset current figure (and eventually axes) to original
67 set (0, "currentfigure", cf); 107 set (0, "currentfigure", cf);
68 if (ancestor (hnew, "figure") == cf && ! isempty (ca)) 108 if (ancestor (hnew(1), "figure") == cf && ! isempty (ca))
69 set (cf, "currentaxes", ca); 109 set (cf, "currentaxes", ca);
70 endif 110 endif
71 111
72 endfunction 112 endfunction
73 113