changeset 10662:3afcd24ced61

legend.m: Key labels for specified objects.
author Ben Abbott <bpabbott@mac.com>
date Wed, 26 May 2010 19:52:40 -0400
parents 3df114a44da3
children b90328525985
files scripts/ChangeLog scripts/plot/legend.m
diffstat 2 files changed, 27 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Wed May 26 19:49:12 2010 -0400
+++ b/scripts/ChangeLog	Wed May 26 19:52:40 2010 -0400
@@ -1,5 +1,6 @@
 2010-05-26  Ben Abbott <bpabbott@mac.com>
 
+	* plot/legend.m: Key labels for specified objects.
 	* plot/ancestor.m: Support vectors of handles.
 
 2010-05-26  Rik <octave@nomad.inbox5.com>
--- a/scripts/plot/legend.m	Wed May 26 19:49:12 2010 -0400
+++ b/scripts/plot/legend.m	Wed May 26 19:52:40 2010 -0400
@@ -24,13 +24,18 @@
 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos})
 ## @deftypefnx {Function File} {} legend (@var{cell})
 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos})
+## @deftypefnx {Function File} {} legend (@var{hax}, @dots{})
+## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{})
+## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{})
 ## @deftypefnx {Function File} {} legend ('@var{func}')
 ##
-## Display a legend for the current axes using the specified strings
-## as labels.  Legend entries may be specified as individual character
-## string arguments, a character array, or a cell array of character
-## strings.  Legend works on line graphs, bar graphs, etc.  A plot must
-## exist before legend is called.
+## Display a legend for the axes with handle @var{hax}, or the current axes,
+## using the specified strings as labels.  Legend entries may be specified 
+## as individual character string arguments, a character array, or a cell
+## array of character strings. If the handles, @var{hobjs}, are not specified
+## the legend's strings will be associated with the axes' descendents.
+## Legend works on line graphs, bar graphs, etc.
+## A plot must exist before legend is called.
 ##
 ## The optional parameter @var{pos} specifies the location of the legend
 ## as follows:
@@ -81,6 +86,15 @@
   [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:});
   nargs = nargin;
 
+  if (all (ishandle (varargin{1})))
+    kids = flipud (varargin{1}(:));
+    varargin(1) = [];
+    nargs = numel (varargin);
+  else
+    kids = get (ca, "children");
+  endif
+  nkids = numel (kids);
+
   if (nargs > 0)
     pos = varargin{nargs};
     if (isnumeric (pos) && isscalar (pos) && round (pos) == pos)
@@ -102,8 +116,6 @@
     endif
   endif
 
-  kids = get (ca, "children");
-  nkids = numel (kids);
   k = 1;
   turn_on_legend = false;
 
@@ -293,3 +305,10 @@
 %! bar (rand (2, 3))
 %! ylim ([0 1.2])
 %! legend ("1st Bar", "2nd Bar", "3rd Bar")
+
+%!demo
+%! clf
+%! x = 0:0.1:7;
+%! h = plot (x, sin(x), x, cos(x), x, sin(x.^2/10), x, cos(x.^2/10));
+%! title ("Only the sin() objects have keylabels")
+%! legend (h([1, 3]), {"sin(x)", "sin(x^2/10)"}, "location", "southwest")