# HG changeset patch # User Ben Abbott # Date 1237654198 14400 # Node ID b3268a0458f23fa19ec384773bee12b4724305c1 # Parent cc916241e811e37187e85ff60006364699f5caac __gnuplot_get_var__.m: Add function to get gnuplot variables. diff -r cc916241e811 -r b3268a0458f2 scripts/ChangeLog --- a/scripts/ChangeLog Sat Mar 21 09:35:35 2009 -0400 +++ b/scripts/ChangeLog Sat Mar 21 12:49:58 2009 -0400 @@ -1,5 +1,6 @@ 2009-03-21 Ben Abbott + * plot/__gnuplot_get_var__.m: Add function to get gnuplot variables. * plot/print.m: Restore the behavior for option -S,. 2009-03-19 Jaroslav Hajek diff -r cc916241e811 -r b3268a0458f2 scripts/plot/Makefile.in --- a/scripts/plot/Makefile.in Sat Mar 21 09:35:35 2009 -0400 +++ b/scripts/plot/Makefile.in Sat Mar 21 12:49:58 2009 -0400 @@ -47,6 +47,7 @@ __errcomm__.m \ __errplot__.m \ __ezplot__.m \ + __gnuplot_get_var__.m \ __gnuplot_has_feature__.m \ __go_close_all__.m \ __go_draw_axes__.m \ diff -r cc916241e811 -r b3268a0458f2 scripts/plot/__gnuplot_get_var__.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/plot/__gnuplot_get_var__.m Sat Mar 21 12:49:58 2009 -0400 @@ -0,0 +1,151 @@ +## Copyright (C) 2009 Ben Abbott +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{value} =} __gnuplot_get_var__ (@var{h}, @var{name}, @var{fmt}) +## Undocumented internal function. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2009-02-07 + +function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt) + + if (nargin == 0) + h = gcf (); + endif + if (nargin < 2) + print_usage (); + endif + if (nargin < 3) + fmt = ''; + endif + + if (numel (h) == 1 && isfigure (h)) + ostream = get (h, "__plot_stream__"); + else + ostream = h; + endif + if (numel (ostream) < 1) + error ("__gnuplot_get_var__: stream to gnuplot not open"); + elseif (ispc ()) # || true + if (numel (ostream) == 1) + error ("__gnuplot_get_var__: Need mkfifo that is not implemented under Windows"); + endif + use_mkfifo = false; + istream = ostream(2); + ostream = ostream(1); + else + use_mkfifo = true; + ostream = ostream(1); + endif + + if (use_mkfifo) + gpin_name = tmpnam (); + + ## Mode: 6*8*8 == 0600 + [err, msg] = mkfifo (gpin_name, 6*8*8); + + if (err != 0) + error ("__gnuplot_get_var__: Can not open fifo (%s)", msg); + endif + endif + + gp_var_name = strtrim (gp_var_name); + n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1; + if (isempty (n)) + n = numel (gp_var_name); + endif + + unwind_protect + + ## Notes: Variables may be undefined if user closes gnuplot by "q" + ## or Alt-F4. Further, this abrupt close also requires the leading + ## "\n" on the next line. + if (use_mkfifo) + fprintf (ostream, "\nset print \"%s\";\n", gpin_name); + fflush (ostream); + [gpin, err] = fopen (gpin_name, "r"); + if (err != 0) + error ("__gnuplot_get_var__: Can not open fifo (%s)", msg); + endif + gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n", + gp_var_name(1:n), gp_var_name); + fputs (ostream, gp_cmd); + + ## Close output file, to force it to be flushed + fputs (ostream, "set print;\n"); + fflush (ostream); + + ## Now read from fifo. + reading = true; + str = {}; + while (reading) + str{end+1} = fgets (gpin); + if (isnumeric (str{end}) && (str{end} == -1)) + reading = false; + str = str(1:(end-1)); + endif + endwhile + str = strcat (str{:}); + fclose (gpin); + else + ## Direct gnuplot to print to + fprintf (ostream, "set print \"-\";\n"); + fflush (ostream); + gp_cmd = sprintf ("\nif (exists(\"%s\")) print \"OCTAVE: \", %s; else print NaN\n", + gp_var_name(1:n), gp_var_name); + fputs (ostream, gp_cmd); + fflush (ostream); + ## Direct gnuplot to print to + fputs (ostream, "set print;\n"); + fflush (ostream); + + str = {}; + while (isempty (str)) + str = char (fread (istream)'); + if (! isempty (str)) + str = regexp (str, "OCTAVE:.*", "match") + str = str{end}(8:end); + endif + fclear (istream); + endwhile + endif + + ## Strip out EOLs and the continuation character "|" + str(str=="\n") = ""; + str(str=="\r") = ""; + n_continue = strfind (str, " \\ "); + if (! isempty (n_continue)) + str(n_continue+1) = ""; + endif + + if (isempty (fmt)) + gp_var_value = strtrim (str); + else + gp_var_value = sscanf (str, fmt); + endif + + unwind_protect_cleanup + if (use_mkfifo) + unlink (gpin_name); + endif + end_unwind_protect + +endfunction +