logo       

clear all and gnuplot (was Weird error with gnuplot: 'invalid command'): msg#00041

gnu.octave.bugs

Subject: clear all and gnuplot (was Weird error with gnuplot: 'invalid command')

On 13-Apr-2005, Alexander Klink <aklink@xxxxxxxxxx> wrote:

| On Tue, Apr 12, 2005 at 10:26:17AM -0400, John W. Eaton wrote:
| > What version of Octave are you using? There have been some recent and
| I am currently using 2.9.1.
|
| > significant changes to the way Octave interacts with gnuplot, so the
| > version probably matters. And in any case, when reporting problems
| > with software, it is a good idea to supply information like that if
| > you would like people to have a reasonable chance of being able to
| > help you.
| Of course you're right here. I realized that I missed providing this
| information right after I left work yesterday :-/
|
| Luckily, today I found out what the difference between my working
| example and the "real" program was: there is a
| clear all;
| line (left from the Matlab code I am porting from, if I understand
| help clear correctly, this might not do the same thing in Octave).
| That is what makes octave crash.
| A simple example is the following
|
| clear all;
| A = [1 2 3 4 5];
| B = [2 4 6 8 10];
| plot (A, B);
|
| klink@pc1299:/igd/a8/home/stud/aklink/matlab/matlab$ octave minimalexample.m
| GNU Octave, version 2.9.1 (i686-pc-linux-gnu).
| Copyright (C) 2005 John W. Eaton.
| [...]
|
| gnuplot> '/tmp/oct-fWxadW' "line 1"
| ^
| line 0: invalid command
|
| The machine here is a Debian sarge where I compiled 2.9.1 from source
| (no additional configure options where used).
| The above code works fine in the 2.1.64 provided by Debian.
| If I leave the "all" out, it works fine as well. IMHO, this is still
| a bug.

Yes, the problem is that with 2.9.x, the interface to gnuplot is all
inside a .oct file so if you clear everything, the .oct file is closed
and the symbols defined in it, including all the internal variables like
"Vgnuplot_command_plot" disappear (though the corresponding variables
like gnuplot_command_plot still appear in the Octave workspace).
Reloading the .oct file (by calling __gnuplot_plot__ again, for
example, does not help, because the gnuplot_init function is not
called (it is called from the PKG_INIT script, which is not run again
when reloading .oct files).

Please try the following patch. It tries to reinitialize the gnuplot
interface if necessary.

There are still some potential problems.

One is that if the .oct file is closed while a connection to gnuplot
is open, the connection is not closed. So the next time you plot
something, it goes into a separate window. Also, I'm not sure that
the resources allocated for handling the plot stream are properly
cleaned up. Perhaps we need to be able to register a list of
functions to run when a .oct file is closed?

Another problem is that some internal variables like the hold state,
or Octave's cached value of the gnuplot terminal type are not
restored. We could fix that by making them built-in variables like
"gnuplot_binary", and adding code to gnuplot_init to restore their
values. Should we do that?

jwe


src/ChangeLog:

2005-04-27 John W. Eaton <jwe@xxxxxxxxxx>

* DLD-FUNCTIONS/gplot.l (gnuplot_init): New function to handle
initialization. If builtin variables have already been installed,
simply update our cached values.
(F__gnuplot_init__): Call gnuplot_init to do all the real work.
(Fclearplot, Fcloseplot, Fhold, Fishold, Fpurge_tmp_files,
F__gnuplot_raw__, F__gnuplot_set__, F__gnuplot_plot__,
F__gnuplot_splot__, F__gnuplot_replot__, Fgplot, Fgsplot, Fgraw,
Fgset, Fgshow): Call gnuplot_init before doing anything.


Index: src/DLD-FUNCTIONS/gplot.l
===================================================================
RCS file: /cvs/octave/src/DLD-FUNCTIONS/gplot.l,v
retrieving revision 1.8
diff -u -r1.8 gplot.l
--- src/DLD-FUNCTIONS/gplot.l 28 Mar 2005 16:05:51 -0000 1.8
+++ src/DLD-FUNCTIONS/gplot.l 27 Apr 2005 20:58:50 -0000
@@ -94,6 +94,8 @@

static inline bool can_be_plotkw (void);

+static void gnuplot_init (void);
+
static bool gpt_quote_is_transpose;
static bool gpt_allow_plotkw;
static int gpt_parens;
@@ -1136,6 +1138,8 @@
{
octave_value_list retval;

+ gnuplot_init ();
+
send_to_plot_stream ("clear\n");

// XXX FIXME XXX -- instead of just clearing these things, it would
@@ -1164,7 +1168,15 @@
@end deftypefn")
{
octave_value_list retval;
+
+ gnuplot_init ();
+
+ parametric_plot = false;
+
+ set_global_value ("__multiplot_mode__", 0.0);
+
close_plot_stream ();
+
return retval;
}

@@ -1188,6 +1200,8 @@
{
octave_value_list retval;

+ gnuplot_init ();
+
int argc = args.length () + 1;

string_vector argv = args.make_argv ("hold");
@@ -1225,6 +1239,8 @@
the plot device will be cleared before drawing the next line.\n\
@end deftypefn")
{
+ gnuplot_init ();
+
return octave_value (! clear_before_plotting);
}

@@ -1243,7 +1259,11 @@
@end deftypefn")
{
octave_value_list retval;
+
+ gnuplot_init ();
+
cleanup_tmp_files ();
+
return retval;
}

@@ -1255,6 +1275,8 @@
{
octave_value_list retval;

+ gnuplot_init ();
+
if (args.length () == 1 && args(0).is_string ())
{
std::string cmd = args(0).string_value ();
@@ -1283,6 +1305,8 @@
{
octave_value_list retval;

+ gnuplot_init ();
+
int argc = args.length () + 1;

string_vector argv = args.make_argv ("set");
@@ -1337,6 +1361,8 @@
{
octave_value_list retval;

+ gnuplot_init ();
+
int argc = args.length () + 1;

string_vector argv = args.make_argv ("show");
@@ -1364,6 +1390,8 @@
DEFUN_DLD (__gnuplot_plot__, args, ,
"Plot with gnuplot.\n")
{
+ gnuplot_init ();
+
doplot ("plot", args);
return octave_value_list ();
}
@@ -1371,6 +1399,8 @@
DEFUN_DLD (__gnuplot_splot__, args, ,
"Plot with gnuplot.\n")
{
+ gnuplot_init ();
+
doplot ("splot", args);
return octave_value_list ();
}
@@ -1378,6 +1408,8 @@
DEFUN_DLD (__gnuplot_replot__, args, ,
"Plot with gnuplot.\n")
{
+ gnuplot_init ();
+
doplot ("replot", args);
return octave_value_list ();
}
@@ -1412,30 +1444,40 @@
DEFUN_DLD (gplot, args, ,
"")
{
+ gnuplot_init ();
+
DEPRECATED_BODY (gplot, __gnuplot_plot__);
}

DEFUN_DLD (gsplot, args, ,
"")
{
+ gnuplot_init ();
+
DEPRECATED_BODY (gsplot, __gnuplot_splot__);
}

DEFUN_DLD (graw, args, ,
"")
{
+ gnuplot_init ();
+
DEPRECATED_BODY (graw, __gnuplot_raw__);
}

DEFUN_DLD (gset, args, ,
"")
{
+ gnuplot_init ();
+
DEPRECATED_BODY (gset, __gnuplot_set__);
}

DEFUN_DLD (gshow, args, ,
"")
{
+ gnuplot_init ();
+
DEPRECATED_BODY (gshow, __gnuplot_show__);
}

@@ -1527,21 +1569,20 @@
return 0;
}

-DEFUN_DLD (__gnuplot_init__, , ,
- "-*- texinfo -*-\n\
-@deftypefn {Loadable Function} __gnuplot_init__ ()\n\
-@end deftypefn")
+static void
+gnuplot_init (void)
{
- octave_value_list retval;
-
static bool gnuplot_initialized = false;

if (gnuplot_initialized)
- return retval;
+ return;

gnuplot_initialized = true;

- DEFVAR (automatic_replot, true, automatic_replot,
+ if (is_builtin_variable ("automatic_replot"))
+ automatic_replot ();
+ else
+ DEFVAR (automatic_replot, true, automatic_replot,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} automatic_replot\n\
You can tell Octave to redisplay the plot each time anything about it\n\
@@ -1551,49 +1592,76 @@
compatibility with Matlab.\n\
@end defvr");

- DEFVAR (gnuplot_binary, GNUPLOT_BINARY, gnuplot_binary,
+ if (is_builtin_variable ("gnuplot_binary"))
+ gnuplot_binary ();
+ else
+ DEFVAR (gnuplot_binary, GNUPLOT_BINARY, gnuplot_binary,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_binary\n\
The name of the program invoked by the plot command. The default value\n\
is @code{\"gnuplot\"}. @xref{Installation}.\n\
@end defvr");

- DEFVAR (gnuplot_command_plot, "pl", gnuplot_command_plot,
+ if (is_builtin_variable ("gnuplot_command_plot"))
+ gnuplot_command_plot ();
+ else
+ DEFVAR (gnuplot_command_plot, "pl", gnuplot_command_plot,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_plot\n\
@end defvr");

- DEFVAR (gnuplot_command_replot, "rep", gnuplot_command_replot,
+ if (is_builtin_variable ("gnuplot_command_replot"))
+ gnuplot_command_replot ();
+ else
+ DEFVAR (gnuplot_command_replot, "rep", gnuplot_command_replot,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_replot\n\
@end defvr");

- DEFVAR (gnuplot_command_splot, "sp", gnuplot_command_splot,
+ if (is_builtin_variable ("gnuplot_command_splot"))
+ gnuplot_command_splot ();
+ else
+ DEFVAR (gnuplot_command_splot, "sp", gnuplot_command_splot,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_splot\n\
@end defvr");

- DEFVAR (gnuplot_command_using, "u", gnuplot_command_using,
+ if (is_builtin_variable ("gnuplot_command_using"))
+ gnuplot_command_using ();
+ else
+ DEFVAR (gnuplot_command_using, "u", gnuplot_command_using,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_using\n\
@end defvr");

- DEFVAR (gnuplot_command_with, "w", gnuplot_command_with,
+ if (is_builtin_variable ("gnuplot_command_with"))
+ gnuplot_command_with ();
+ else
+ DEFVAR (gnuplot_command_with, "w", gnuplot_command_with,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_with\n\
@end defvr");

- DEFVAR (gnuplot_command_axes, "ax", gnuplot_command_axes,
+ if (is_builtin_variable ("gnuplot_command_axes"))
+ gnuplot_command_axes ();
+ else
+ DEFVAR (gnuplot_command_axes, "ax", gnuplot_command_axes,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_axes\n\
@end defvr");

- DEFVAR (gnuplot_command_title, "t", gnuplot_command_title,
+ if (is_builtin_variable ("gnuplot_command_title"))
+ gnuplot_command_title ();
+ else
+ DEFVAR (gnuplot_command_title, "t", gnuplot_command_title,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_title\n\
@end defvr");

- DEFVAR (gnuplot_command_end, "\n", gnuplot_command_end,
+ if (is_builtin_variable ("gnuplot_command_end"))
+ gnuplot_command_end ();
+ else
+ DEFVAR (gnuplot_command_end, "\n", gnuplot_command_end,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_command_end\n\
@end defvr");
@@ -1604,7 +1672,10 @@
bool with_frames = false;
#endif

- DEFVAR (gnuplot_has_frames, with_frames, gnuplot_has_frames,
+ if (is_builtin_variable ("gnuplot_has_frames"))
+ gnuplot_has_frames ();
+ else
+ DEFVAR (gnuplot_has_frames, with_frames, gnuplot_has_frames,
"-*- texinfo -*-\n\
@defvr {Built-in Variable} gnuplot_has_frames\n\
If the value of this variable is nonzero, Octave assumes that your copy\n\
@@ -1614,5 +1685,16 @@
configure got it wrong, or if you upgrade your gnuplot installation.\n\
@end defvr");

+}
+
+DEFUN_DLD (__gnuplot_init__, , ,
+ "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} __gnuplot_init__ ()\n\
+@end deftypefn")
+{
+ octave_value_list retval;
+
+ gnuplot_init ();
+
return retval;
}



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise