diff scripts/plot/legend.m @ 11246:c463aed850b0

allow legend and gcf to work when no figure is present
author John W. Eaton <jwe@octave.org>
date Sat, 13 Nov 2010 01:25:05 -0500
parents abc0c6b0a4c4
children a0dfd7e8e3e2
line wrap: on
line diff
--- a/scripts/plot/legend.m	Fri Nov 12 20:53:32 2010 -0500
+++ b/scripts/plot/legend.m	Sat Nov 13 01:25:05 2010 -0500
@@ -100,13 +100,18 @@
 
 function [hlegend2, hobjects2, hplot2, text_strings2] = legend (varargin)
 
-  if (! ishandle (varargin {1}) || (strcmp (get (varargin {1}, "type"), "axes")
-      && ! strcmp (get (varargin {1}, "tag"), "legend")))
+  if (nargin > 0
+      && (! ishandle (varargin{1})
+          || (strcmp (get (varargin{1}, "type"), "axes")
+              && ! strcmp (get (varargin{1}, "tag"), "legend"))))
     [ca, varargin, nargs] = __plt_get_axis_arg__ ("legend", varargin{:});
     fig = get (ca, "parent");
   else
     fig = get (0, "currentfigure");
-    ca = get (fig, "currentaxes");
+    if (isempty (fig))
+      fig = gcf ();
+    endif
+    ca = gca ();
   endif
 
   if (strcmp (get (ca, "tag"), "plotyy"))
@@ -118,10 +123,9 @@
     endif
   endif
 
-  if (all (ishandle (varargin{1})))
+  if (nargin > 0 && all (ishandle (varargin{1})))
     kids = flipud (varargin{1}(:));
     varargin(1) = [];
-    nargs = numel (varargin);
   else
     kids = ca;
     kids (strcmp (get (ca, "tag"), "legend")) = [];
@@ -131,6 +135,7 @@
       kids = [get(kids, "children"){:}](:);
     endif
   endif
+  nargs = numel (varargin);
   nkids = numel (kids);
 
   orientation = "default";