changeset 27791:188fb5415ab5

title.m: Allow a legend handle as first argument (bug #57372). * title.m: Update documentation to mention that a legend handle can be the first argument. Add special code to detect a legend handle as first argument. Add BIST test for bug #57372.
author Rik <rik@octave.org>
date Sun, 08 Dec 2019 11:11:00 -0800
parents 3f5026fd8da8
children 5169ed0ff0f0
files scripts/plot/appearance/title.m
diffstat 1 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/appearance/title.m	Wed Dec 04 22:42:47 2019 +0100
+++ b/scripts/plot/appearance/title.m	Sun Dec 08 11:11:00 2019 -0800
@@ -26,8 +26,8 @@
 ## An optional list of @var{property}/@var{value} pairs can be used to change
 ## the appearance of the created title text object.
 ##
-## If the first argument @var{hax} is an axes handle, then plot into this axes,
-## rather than the current axes returned by @code{gca}.
+## If the first argument @var{hax} is an axes or legend handle, then add a
+## title to this object, rather than the current axes returned by @code{gca}.
 ##
 ## The optional return value @var{h} is a graphics handle to the created text
 ## object.
@@ -41,7 +41,15 @@
   [hax, varargin, nargin] = __plt_get_axis_arg__ ("title", varargin{:});
 
   if (isempty (hax))
-    hax = gca ();
+    if (! isempty (varargin) && isscalar (varargin{1})
+        && ishghandle (varargin{1})
+        && strcmp (get (varargin{1}, "tag"), "legend"))
+        hax = varargin{1};
+        varargin(1) = [];
+        nargin--;
+    else
+      hax = gca ();
+    endif
   endif
 
   if (rem (nargin, 2) != 1)
@@ -124,3 +132,15 @@
 %! unwind_protect_cleanup
 %!   close (hf);
 %! end_unwind_protect
+
+%!test <*57372>
+%! hf = figure ("visible", "off");
+%! unwind_protect
+%!   plot (1:10);
+%!   hl = legend ("legend text");
+%!   ht = title (hl, "Legend Title");
+%!   assert (get (ht, "string"), "Legend Title");
+%!   assert (get (ht, "parent"), hl);
+%! unwind_protect_cleanup
+%!   close (hf);
+%! end_unwind_protect