changeset 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 3cadca91e390
children 225d88a97104
files scripts/plot/util/copyobj.m
diffstat 1 files changed, 52 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/util/copyobj.m	Tue Apr 12 21:05:50 2016 +1000
+++ b/scripts/plot/util/copyobj.m	Thu Apr 14 12:51:43 2016 -0700
@@ -19,11 +19,19 @@
 ## -*- texinfo -*-
 ## @deftypefn  {} {@var{hnew} =} copyobj (@var{horig})
 ## @deftypefnx {} {@var{hnew} =} copyobj (@var{horig}, @var{hparent})
-## Construct a copy of the graphic object associated with handle @var{horig}
-## and return a handle @var{hnew} to the new object.
+## Construct a copy of the graphic objects associated with the handles
+## @var{horig} and return new handles @var{hnew} to the new objects.
 ##
 ## If a parent handle @var{hparent} (root, figure, axes, or hggroup) is
 ## specified, the copied object will be created as a child of @var{hparent}.
+##
+## If @var{horig} is a vector of handles, and @var{hparent} is a scalar,
+## then each handle in the vector @var{hnew} has its @qcode{"Parent"} property
+## set to @var{hparent}.  Conversely, if @var{horig} is a scalar and
+## @var{hparent} a vector, then each parent object will receive a copy of
+## @var{horig}.  If @var{horig} and @var{hparent} are both vectors with the
+## same number of elements then @code{@var{hnew}(i)} will have parent
+## @code{@var{hparent}(i)}.
 ## @seealso{struct2hdl, hdl2struct, findobj}
 ## @end deftypefn
 
@@ -40,8 +48,17 @@
     print_usage ();
   elseif (! ishandle (hparent))
     hparent = figure (fix (hparent));
-  elseif (! any (strcmpi (get (hparent).type, partypes)))
-    print_usage ();
+  else
+    for hp = hparent(:)'
+      if (! any (strcmpi (get (hp, "type"), partypes)))
+        print_usage ();
+      endif
+    endfor
+  endif
+
+  if (numel (horig) != numel (hparent)
+      && ! (isscalar (hparent) || isscalar (horig)))
+    error ("copyobj: size of HORIG and HPARENT must match, or one must be a scalar");
   endif
 
   ## current figure and axes
@@ -49,23 +66,46 @@
   ca = get (cf, "currentaxes");
 
   ## compatibility of input handles
-  kididx = find (strcmp (alltypes, get (horig).type));
-  paridx = find (strcmp (alltypes, get (hparent).type));
+  for i = 1:numel (horig)
+    kididx(i) = find (strcmp (alltypes, get (horig(i), "type"))); 
+  endfor
+
+  for i = 1:numel (hparent)
+    paridx(i) = find (strcmp (alltypes, get (hparent(i), "type"))); 
+  endfor
 
   if (kididx <= paridx)
-    error ("copyobj: %s object can't be children to %s.",
+    error ("copyobj: %s object can't be a child of %s.",
            alltypes{kididx}, alltypes{paridx});
-  elseif (nargin == 1)
+  endif
+
+  ## loop over vector inputs
+  if (nargin == 1)
+    ## No parent specified
+    for i = numel (horig)
+      str = hdl2struct (horig(i));
+      hnew(i) = struct2hdl (str);
+    endfor
+  elseif (isscalar (hparent))
+    for i = 1:numel (horig)
+      str = hdl2struct (horig(i));
+      hnew(i) = struct2hdl (str, hparent);
+    endfor
+  elseif (isscalar (horig))
     str = hdl2struct (horig);
-    hnew = struct2hdl (str);
+    for i = 1:numel (hparent)
+      hnew(i) = struct2hdl (str, hparent(i));
+    endfor
   else
-    str = hdl2struct (horig);
-    hnew = struct2hdl (str, hparent);
+    for i = 1:numel (horig)
+      str = hdl2struct (horig(i));
+      hnew(i) = struct2hdl (str, hparent(i));
+    endfor
   endif
 
   ## reset current figure (and eventually axes) to original
   set (0, "currentfigure", cf);
-  if (ancestor (hnew, "figure") == cf && ! isempty (ca))
+  if (ancestor (hnew(1), "figure") == cf && ! isempty (ca))
     set (cf, "currentaxes", ca);
   endif