comparison scripts/plot/private/__gnuplot_get_var__.m @ 16108:3cce6b4e0f7c

Update gnuplot plotting scripts for faster or more modern synta * scripts/plot/__gnuplot_drawnow__.m: Use default values in function header. Check number of arguments to function immediately. Use single quotes to avoid lots of backslashing. Use strtok() to replace hand-coded functionality. Initialize persistent variables in declaration (10% faster). * scripts/plot/gnuplot_binary.in: Replace tabs with spaces. Simplify input validation. Add %!test block. * scripts/plot/private/__gnuplot_get_var__.m: Use default values in function header. Use "*char" in fread to automatically convert to char variable. * scripts/plot/private/__gnuplot_ginput__.m: Check immediately for required version of gnuplot. Use "*char" in fread to automatically convert to char variable. Use fputs in place of fprintf to match rest of code. * scripts/plot/private/__gnuplot_has_feature__.m: Initialize persistent varibles in declaration. Use false () rather than logical (zeros()) construction. * scripts/plot/private/__gnuplot_has_terminal__.m: Use strtok() to replace hand-coded functionality * scripts/plot/private/__gnuplot_print__.m: Replace sprintf calls with direct string char matrix concatenation (2.4x faster) where possible. Replace for loop with multiple argument form of set(). Use single quotes to avoid lots of backslashing. * scripts/plot/private/__gnuplot_version__.m: Use single quotes to avoid lots of backslashing.
author Rik <rik@octave.org>
date Mon, 25 Feb 2013 21:01:36 -0800
parents 72c96de7a403
children abf384f5d243
comparison
equal deleted inserted replaced
16107:3b791008b88e 16108:3cce6b4e0f7c
22 ## @end deftypefn 22 ## @end deftypefn
23 23
24 ## Author: Ben Abbott <bpabbott@mac.com> 24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2009-02-07 25 ## Created: 2009-02-07
26 26
27 function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt) 27 function gp_var_value = __gnuplot_get_var__ (h, gp_var_name, fmt = "")
28 28
29 if (nargin == 0)
30 h = gcf ();
31 endif
32 if (nargin < 2) 29 if (nargin < 2)
33 print_usage (); 30 print_usage ();
34 endif
35 if (nargin < 3)
36 fmt = '';
37 endif 31 endif
38 32
39 if (numel (h) == 1 && isfigure (h)) 33 if (numel (h) == 1 && isfigure (h))
40 if (isempty (get (gcf, "__plot_stream__"))) 34 if (isempty (get (gcf, "__plot_stream__")))
41 ostream = __gnuplot_open_stream__ (2, h); 35 ostream = __gnuplot_open_stream__ (2, h);
63 gpin_name = tmpnam (); 57 gpin_name = tmpnam ();
64 58
65 ## Mode: 6*8*8 == 0600 59 ## Mode: 6*8*8 == 0600
66 [err, msg] = mkfifo (gpin_name, 6*8*8); 60 [err, msg] = mkfifo (gpin_name, 6*8*8);
67 61
68 if (err != 0) 62 if (err)
69 error ("__gnuplot_get_var__: Can not make fifo (%s)", msg); 63 error ("__gnuplot_get_var__: Can not make FIFO (%s)", msg);
70 endif 64 endif
71 endif 65 endif
72 66
73 gp_var_name = strtrim (gp_var_name); 67 gp_var_name = strtrim (gp_var_name);
74 n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1; 68 n = min (strfind (gp_var_name, " "), strfind (gp_var_name, ",")) - 1;
77 endif 71 endif
78 72
79 unwind_protect 73 unwind_protect
80 74
81 ## Notes: Variables may be undefined if user closes gnuplot by "q" 75 ## Notes: Variables may be undefined if user closes gnuplot by "q"
82 ## or Alt-F4. Further, this abrupt close also requires the leading 76 ## or Alt-F4. Further, this abrupt close also requires the leading
83 ## "\n" on the next line. 77 ## "\n" on the next line.
84 if (use_mkfifo) 78 if (use_mkfifo)
85 fprintf (ostream, "\nset print \"%s\";\n", gpin_name); 79 fprintf (ostream, "\nset print \"%s\";\n", gpin_name);
86 fflush (ostream); 80 fflush (ostream);
87 [gpin, err] = fopen (gpin_name, "r"); 81 [gpin, err] = fopen (gpin_name, "r");
88 if (err != 0) 82 if (err)
89 ## Try a second time, and then give an error. 83 ## Try a second time, and then give an error.
90 [gpin, err] = fopen (gpin_name, "r"); 84 [gpin, err] = fopen (gpin_name, "r");
91 endif 85 endif
92 if (err != 0) 86 if (err)
93 error ("__gnuplot_get_var__: can not open fifo"); 87 error ("__gnuplot_get_var__: can not open FIFO");
94 endif 88 endif
95 gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n", 89 gp_cmd = sprintf ("\nif (exists(\"%s\")) print %s; else print NaN\n",
96 gp_var_name(1:n), gp_var_name); 90 gp_var_name(1:n), gp_var_name);
97 fputs (ostream, gp_cmd); 91 fputs (ostream, gp_cmd);
98 92
124 fputs (ostream, "set print;\n"); 118 fputs (ostream, "set print;\n");
125 fflush (ostream); 119 fflush (ostream);
126 120
127 str = {}; 121 str = {};
128 while (isempty (str)) 122 while (isempty (str))
129 str = char (fread (istream)'); 123 str = fread (istream, "*char")';
130 if (isempty (str)) 124 if (isempty (str))
131 sleep (0.05); 125 sleep (0.05);
132 else 126 else
133 str = regexp (str, 'OCTAVE:.*', "match"); 127 str = regexp (str, 'OCTAVE:.*', "match");
134 str = str{end}(8:end); 128 str = str{end}(8:end);
136 fclear (istream); 130 fclear (istream);
137 endwhile 131 endwhile
138 endif 132 endif
139 133
140 ## Strip out EOLs and the continuation character "|" 134 ## Strip out EOLs and the continuation character "|"
141 str(str=="\n") = ""; 135 str(str=="\n" | str=="\r") = "";
142 str(str=="\r") = "";
143 n_continue = strfind (str, " \\ "); 136 n_continue = strfind (str, " \\ ");
144 if (! isempty (n_continue)) 137 if (! isempty (n_continue))
145 str(n_continue+1) = ""; 138 str(n_continue+1) = "";
146 endif 139 endif
147 140