1 ## Copyright (C) 2009-2012 Ben Abbott
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{value} =} __gnuplot_get_var__ (@var{h}, @var{name}, @var{fmt})
21 ## Undocumented internal function.
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2009-02-07
27 function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt)
39 if (numel (h) == 1 && isfigure (h))
40 if (isempty (get (gcf, "__plot_stream__")))
41 ostream = __gnuplot_open_stream__ (2, h);
43 ostream = get (h, "__plot_stream__");
48 if (numel (ostream) < 1)
49 error ("__gnuplot_get_var__: stream to gnuplot not open");
51 if (numel (ostream) == 1)
52 error ("__gnuplot_get_var__: Need mkfifo that is not implemented under Windows");
63 gpin_name = tmpnam ();
65 ## Mode: 6*8*8 == 0600
66 [err, msg] = mkfifo (gpin_name, 6*8*8);
69 error ("__gnuplot_get_var__: Can not make fifo (%s)", msg);
73 gp_var_name = strtrim (gp_var_name);
74 n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1;
76 n = numel (gp_var_name);
81 ## Notes: Variables may be undefined if user closes gnuplot by "q"
82 ## or Alt-F4. Further, this abrupt close also requires the leading
83 ## "\n" on the next line.
85 fprintf (ostream, "\nset print \"%s\";\n", gpin_name);
87 [gpin, err] = fopen (gpin_name, "r");
89 ## Try a second time, and then give an error.
90 [gpin, err] = fopen (gpin_name, "r");
93 error ("__gnuplot_get_var__: can not open fifo");
95 gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n",
96 gp_var_name(1:n), gp_var_name);
97 fputs (ostream, gp_cmd);
99 ## Close output file, to force it to be flushed
100 fputs (ostream, "set print;\n");
103 ## Now read from fifo.
107 str{end+1} = fgets (gpin);
108 if (isnumeric (str{end}) && (str{end} == -1))
110 str = str(1:(end-1));
113 str = strcat (str{:});
116 ## Direct gnuplot to print to <STDOUT>
117 fprintf (ostream, "set print \"-\";\n");
119 gp_cmd = sprintf ("\nif (exists(\"%s\")) print \"OCTAVE: \", %s; else print NaN\n",
120 gp_var_name(1:n), gp_var_name);
121 fputs (ostream, gp_cmd);
123 ## Direct gnuplot to print to <STDERR>
124 fputs (ostream, "set print;\n");
128 while (isempty (str))
129 str = char (fread (istream)');
133 str = regexp (str, 'OCTAVE:.*', "match");
134 str = str{end}(8:end);
140 ## Strip out EOLs and the continuation character "|"
143 n_continue = strfind (str, " \\ ");
144 if (! isempty (n_continue))
145 str(n_continue+1) = "";
149 gp_var_value = strtrim (str);
151 gp_var_value = sscanf (str, fmt);
154 unwind_protect_cleanup