comparison scripts/plot/__gnuplot_get_var__.m @ 9001:b3268a0458f2

__gnuplot_get_var__.m: Add function to get gnuplot variables.
author Ben Abbott <bpabbott@mac.com>
date Sat, 21 Mar 2009 12:49:58 -0400
parents
children 3a5d41b382ab
comparison
equal deleted inserted replaced
9000:cc916241e811 9001:b3268a0458f2
1 ## Copyright (C) 2009 Ben Abbott
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{value} =} __gnuplot_get_var__ (@var{h}, @var{name}, @var{fmt})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2009-02-07
26
27 function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt)
28
29 if (nargin == 0)
30 h = gcf ();
31 endif
32 if (nargin < 2)
33 print_usage ();
34 endif
35 if (nargin < 3)
36 fmt = '';
37 endif
38
39 if (numel (h) == 1 && isfigure (h))
40 ostream = get (h, "__plot_stream__");
41 else
42 ostream = h;
43 endif
44 if (numel (ostream) < 1)
45 error ("__gnuplot_get_var__: stream to gnuplot not open");
46 elseif (ispc ()) # || true
47 if (numel (ostream) == 1)
48 error ("__gnuplot_get_var__: Need mkfifo that is not implemented under Windows");
49 endif
50 use_mkfifo = false;
51 istream = ostream(2);
52 ostream = ostream(1);
53 else
54 use_mkfifo = true;
55 ostream = ostream(1);
56 endif
57
58 if (use_mkfifo)
59 gpin_name = tmpnam ();
60
61 ## Mode: 6*8*8 == 0600
62 [err, msg] = mkfifo (gpin_name, 6*8*8);
63
64 if (err != 0)
65 error ("__gnuplot_get_var__: Can not open fifo (%s)", msg);
66 endif
67 endif
68
69 gp_var_name = strtrim (gp_var_name);
70 n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1;
71 if (isempty (n))
72 n = numel (gp_var_name);
73 endif
74
75 unwind_protect
76
77 ## Notes: Variables may be undefined if user closes gnuplot by "q"
78 ## or Alt-F4. Further, this abrupt close also requires the leading
79 ## "\n" on the next line.
80 if (use_mkfifo)
81 fprintf (ostream, "\nset print \"%s\";\n", gpin_name);
82 fflush (ostream);
83 [gpin, err] = fopen (gpin_name, "r");
84 if (err != 0)
85 error ("__gnuplot_get_var__: Can not open fifo (%s)", msg);
86 endif
87 gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n",
88 gp_var_name(1:n), gp_var_name);
89 fputs (ostream, gp_cmd);
90
91 ## Close output file, to force it to be flushed
92 fputs (ostream, "set print;\n");
93 fflush (ostream);
94
95 ## Now read from fifo.
96 reading = true;
97 str = {};
98 while (reading)
99 str{end+1} = fgets (gpin);
100 if (isnumeric (str{end}) && (str{end} == -1))
101 reading = false;
102 str = str(1:(end-1));
103 endif
104 endwhile
105 str = strcat (str{:});
106 fclose (gpin);
107 else
108 ## Direct gnuplot to print to <STDOUT>
109 fprintf (ostream, "set print \"-\";\n");
110 fflush (ostream);
111 gp_cmd = sprintf ("\nif (exists(\"%s\")) print \"OCTAVE: \", %s; else print NaN\n",
112 gp_var_name(1:n), gp_var_name);
113 fputs (ostream, gp_cmd);
114 fflush (ostream);
115 ## Direct gnuplot to print to <STDERR>
116 fputs (ostream, "set print;\n");
117 fflush (ostream);
118
119 str = {};
120 while (isempty (str))
121 str = char (fread (istream)');
122 if (! isempty (str))
123 str = regexp (str, "OCTAVE:.*", "match")
124 str = str{end}(8:end);
125 endif
126 fclear (istream);
127 endwhile
128 endif
129
130 ## Strip out EOLs and the continuation character "|"
131 str(str=="\n") = "";
132 str(str=="\r") = "";
133 n_continue = strfind (str, " \\ ");
134 if (! isempty (n_continue))
135 str(n_continue+1) = "";
136 endif
137
138 if (isempty (fmt))
139 gp_var_value = strtrim (str);
140 else
141 gp_var_value = sscanf (str, fmt);
142 endif
143
144 unwind_protect_cleanup
145 if (use_mkfifo)
146 unlink (gpin_name);
147 endif
148 end_unwind_protect
149
150 endfunction
151