changeset 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 6f5d92d55acd
children 29b1f7d68665
files scripts/ChangeLog scripts/plot/gnuplot_drawnow.m scripts/plot/legend.m
diffstat 3 files changed, 31 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Nov 12 20:53:32 2010 -0500
+++ b/scripts/ChangeLog	Sat Nov 13 01:25:05 2010 -0500
@@ -1,3 +1,9 @@
+2010-11-13  John W. Eaton  <jwe@octave.org>
+
+	* plot/gnuplot_drawnow.m (have_non_legend_axes): New function.
+	Use it to avoid errors if there are no axes.
+	* plot/legend.m: Avoid indexing beyond end of varargin array.
+
 2010-11-10  John W. Eaton  <jwe@octave.org>
 
 	* gethelp.cc: Eliminate special case for __DECCXX.
--- a/scripts/plot/gnuplot_drawnow.m	Fri Nov 12 20:53:32 2010 -0500
+++ b/scripts/plot/gnuplot_drawnow.m	Sat Nov 13 01:25:05 2010 -0500
@@ -302,9 +302,9 @@
     fputs (plot_stream, "unset multiplot;\n");
     flickering_terms = {"x11", "windows", "wxt", "dumb"};
     if (! any (strcmp (term, flickering_terms))
-        || (numel (findall (h, "type", "axes")) -
-            sum (strcmp (get (findall (h, "type", "axes"), "tag"), "legend"))) > 1
+        || have_non_legend_axes (h)
         || numel (findall (h, "type", "image")) > 0)
+1
       fprintf (plot_stream, "%s\n", term_str);
       if (nargin == 5)
         if (! isempty (file))
@@ -377,4 +377,16 @@
   ret = any (strcmpi ({"aqua", "dumb", "wxt", "x11", "windows", "pm"}, term));
 endfunction
 
-
+function retval = have_non_legend_axes (h)
+  retval = false;
+  all_axes = findall (h, "type", "axes");
+  if (! isempty (all_axes))
+    n_all_axes = numel (all_axes);
+    all_axes_tags = get (all_axes, "tag");
+    legend_axes = strcmp (all_axes_tags, "legend");
+    if (! isempty (legend_axes))
+      n_legend_axes = sum (legend_axes);
+      retval = (n_all_axes - n_legend_axes) > 1;
+    endif
+  endif
+endfunction
--- 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";