octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnuplot output from print?


From: John W. Eaton
Subject: Re: gnuplot output from print?
Date: Thu, 06 Sep 2007 16:06:36 -0400

On  6-Sep-2007, John W. Eaton wrote:

| On  1-Sep-2007, Shai Ayal wrote:
| 
| | On 9/1/07, Søren Hauberg <address@hidden> wrote:
| | > Hi,
| | >   I don't really know that much about how the current plotting system
| | > work, so this will probably be a very silly question. I was just
| | > wondering how hard it would be to add gnuplot output to print. That is,
| | > making it possible to write something like,
| | >
| | >   print -dgnuplot myplot.gp
| | >
| | > which then would produce a file gnuplot could read. This could be quite
| | > useful when you want to tweak some minor detail of a plot. Anyway, just
| | > thinking out loud...
| | 
| | Very nice suggestion. I am not near the computer now, but I think
| | that, taking example from drawnow.m:
| | 
| | fid = fopen("out.gplt","wb");
| | __go_draw_figure__ (fig_handle, fid);
| | fclose(fid);
| | 
| | Should give you what you want
| 
| Oh, that's a nice trick.  I think it is a much better way of saving
| the gnuplot command stream than doing something like
| 
|   gnuplot_binary("tee out.gplt | gnuplot");
| 
| and then doing plotting, which is what I used to do.
| 
| If someone is interested in a small project, it would be nice to fold
| this functionality into drawnow, though I'm not sure what the
| interface should be.  Perhaps
| 
|   drawnow (term, file, dump_commands)
| 
| Note that it should probably avoid writing "set output" and "set term"
| commands to the output file.

Since I find that I often need to see precisely what commands Octave
is sending to gnuplot (see the bug report from today about line colors
vs the -mono option for the print command) I made the following
changes.  Now if you use the option -debug=FILE with the print
command, Octave will also write all the commands it sends to gnuplot
to the file FILE (with just -debug, it writes to the file
octave-print-commands.log).

jwe


scripts/ChangeLog:

2007-09-06  John W. Eaton  <address@hidden>

        * plot/drawnow.m (drawnow): New arg, debug_file.
        (init_plot_stream): Split from open_plot_stream.
        * plot/print.m: Accept -debug=FILE argument.


Index: scripts/plot/drawnow.m
===================================================================
RCS file: /cvs/octave/scripts/plot/drawnow.m,v
retrieving revision 1.21
diff -u -u -r1.21 drawnow.m
--- scripts/plot/drawnow.m      6 Sep 2007 05:55:14 -0000       1.21
+++ scripts/plot/drawnow.m      6 Sep 2007 20:05:10 -0000
@@ -24,7 +24,7 @@
 
 ## Author: jwe
 
-function drawnow (term, file)
+function drawnow (term, file, debug_file)
 
   persistent drawnow_executing = 0;
 
@@ -35,18 +35,27 @@
       return;
     endif
 
-    if (nargin == 2)
+    if (nargin == 2 || nargin == 3)
       h = get (0, "currentfigure");
       if (h)
        f = get (h);
        plot_stream = [];
+       fid = [];
        unwind_protect
          plot_stream = open_gnuplot_stream ([], term, file);
          __go_draw_figure__ (f, plot_stream);
+         if (nargin == 3)
+           fid = fopen (debug_file, "wb");
+           init_plot_stream (fid, [], term, file);
+           __go_draw_figure__ (f, fid);
+         endif
        unwind_protect_cleanup
          if (! isempty (plot_stream))
            pclose (plot_stream);
          endif
+         if (! isempty (fid))
+           fclose (fid);
+         endif
        end_unwind_protect
       else
        error ("drawnow: nothing to draw");
@@ -84,7 +93,7 @@
 
 endfunction
 
-function plot_stream = open_gnuplot_stream (h, term, file)
+function plot_stream = open_gnuplot_stream (h, varargin)
 
   ## If drawnow is cleared, it is possible to register __go_close_all__
   ## more than once, but that is not fatal.
@@ -102,52 +111,62 @@
       set (h, "__plot_stream__", plot_stream);
     endif
 
-    if (nargin == 3)
-      fprintf (plot_stream, "set terminal %s;\n", term);
-      fprintf (plot_stream, "set output \"%s\";\n", file);
-    else
+    init_plot_stream (plot_stream, h, varargin{:})
 
-      ## Guess the terminal type.
-      term = getenv ("GNUTERM");
-      if (isempty (term))
-       if (! isempty (getenv ("DISPLAY")))
-          term = "x11";
-       elseif (! isunix ())
-          term = "windows";
-       else
-         ## This should really be checking for os x before setting
-         ## the terminal type to aqua, but nobody will notice because
-         ## every other unix will be using x11 and windows will be
-         ## using windows.  Those diehards still running octave from
-         ## a linux console know how to set the GNUTERM variable.
-          term = "aqua";
-       endif
-      endif
+    if (isempty (__go_close_all_registered__))
+      atexit ("__go_close_all__");
+      __go_close_all_registered__ = true;
+    endif
 
-      ## If no 'h' (why not?) then open the terminal as Figure 0.
-      if (isempty (h))
-        h = 0;
-      endif
+  endif
 
-      if (strcmp (term, "x11"))
-        fprintf (plot_stream, "set terminal x11 title \"Figure %d\"\n", h);
-      elseif (strcmp (term, "aqua"))
-        ## Aqua doesn't understand the 'title' option despite what the
-        ## gnuplot 4.2 documentation says.
-        fprintf (plot_stream, "set terminal aqua %d\n", h);
-      elseif (strcmp (term, "wxt"))
-        fprintf (plot_stream, "set terminal wxt title \"Figure %d\"\n", h);
+endfunction
+
+function init_plot_stream (plot_stream, h, term, file)
+
+  if (nargin == 4)
+    if (! isempty (term))
+      fprintf (plot_stream, "set terminal %s;\n", term);
+    endif
+    if (! isempty (file))
+      fprintf (plot_stream, "set output \"%s\";\n", file);
+    endif
+  else
+
+    ## Guess the terminal type.
+    term = getenv ("GNUTERM");
+    if (isempty (term))
+      if (! isempty (getenv ("DISPLAY")))
+       term = "x11";
+      elseif (! isunix ())
+       term = "windows";
+      else
+       ## This should really be checking for os x before setting
+       ## the terminal type to aqua, but nobody will notice because
+       ## every other unix will be using x11 and windows will be
+       ## using windows.  Those diehards still running octave from
+       ## a linux console know how to set the GNUTERM variable.
+       term = "aqua";
       endif
-      ## gnuplot will pick up the GNUTERM environment variable itself
-      ## so no need to set the terminal type if not also setting the
-      ## figure title.
+    endif
 
+    ## If no 'h' (why not?) then open the terminal as Figure 0.
+    if (isempty (h))
+      h = 0;
     endif
 
-    if (isempty (__go_close_all_registered__))
-      atexit ("__go_close_all__");
-      __go_close_all_registered__ = true;
+    if (strcmp (term, "x11"))
+      fprintf (plot_stream, "set terminal x11 title \"Figure %d\"\n", h);
+    elseif (strcmp (term, "aqua"))
+      ## Aqua doesn't understand the 'title' option despite what the
+      ## gnuplot 4.2 documentation says.
+      fprintf (plot_stream, "set terminal aqua %d\n", h);
+    elseif (strcmp (term, "wxt"))
+      fprintf (plot_stream, "set terminal wxt title \"Figure %d\"\n", h);
     endif
+    ## gnuplot will pick up the GNUTERM environment variable itself
+    ## so no need to set the terminal type if not also setting the
+    ## figure title.
 
   endif
 
Index: scripts/plot/print.m
===================================================================
RCS file: /cvs/octave/scripts/plot/print.m,v
retrieving revision 1.20
diff -u -u -r1.20 print.m
--- scripts/plot/print.m        15 Jun 2007 21:59:16 -0000      1.20
+++ scripts/plot/print.m        6 Sep 2007 20:05:10 -0000
@@ -119,6 +119,8 @@
   name = "";
   devopt = "";
   printer = "";
+  debug = false;
+  debug_file = "octave-print-commands.log"
 
   for i = 1:nargin
     arg = varargin{i};
@@ -135,8 +137,13 @@
        orientation = "portrait";
       elseif (strcmp (arg, "-landscape"))
        orientation = "landscape";
+      elseif (strncmp (arg, "-debug", 6))
+       debug = true;
+       if (length (arg) > 7)
+         debug_file = arg(7:end);
+       endif
       elseif (length (arg) > 2 && arg(1:2) == "-d")
-       devopt = arg(3:length(arg));
+       devopt = arg(3:end);
       elseif (length (arg) > 2 && arg(1:2) == "-P")
        printer = arg;
       elseif (length (arg) > 2 && arg(1:2) == "-F")
@@ -332,7 +339,11 @@
     new_terminal = dev;
   endif
 
-  drawnow (new_terminal, name);
+  if (debug)
+    drawnow (new_terminal, name, debug_file);
+  else
+    drawnow (new_terminal, name);
+  endif
 
   if (! isempty (convertname))
     command = sprintf ("convert '%s' '%s'", name, convertname);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]