comparison scripts/plot/__gnuplot_drawnow__.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 1339c964b527
children b672afbb7c3c
comparison
equal deleted inserted replaced
16107:3b791008b88e 16108:3cce6b4e0f7c
21 ## Undocumented internal function. 21 ## Undocumented internal function.
22 ## @end deftypefn 22 ## @end deftypefn
23 23
24 ## Author: jwe 24 ## Author: jwe
25 25
26 function __gnuplot_drawnow__ (h, term, file, mono, debug_file) 26 function __gnuplot_drawnow__ (h, term, file, mono = false, debug_file)
27 27
28 if (nargin < 4) 28 if (nargin < 1 || nargin > 5 || nargin == 2)
29 mono = false; 29 print_usage ();
30 endif 30 endif
31 31
32 if (nargin >= 3 && nargin <= 5) 32 if (nargin >= 3 && nargin <= 5)
33 ## Produce various output formats, or redirect gnuplot stream to a 33 ## Produce various output formats, or redirect gnuplot stream to a
34 ## debug file. 34 ## debug file.
37 default_plot_stream = get (h, "__plot_stream__"); 37 default_plot_stream = get (h, "__plot_stream__");
38 unwind_protect 38 unwind_protect
39 plot_stream = __gnuplot_open_stream__ (2, h); 39 plot_stream = __gnuplot_open_stream__ (2, h);
40 gnuplot_supports_term = __gnuplot_has_terminal__ (term, plot_stream); 40 gnuplot_supports_term = __gnuplot_has_terminal__ (term, plot_stream);
41 if (gnuplot_supports_term) 41 if (gnuplot_supports_term)
42 enhanced = gnuplot_set_term (plot_stream (1), true, h, term, file); 42 enhanced = gnuplot_set_term (plot_stream(1), true, h, term, file);
43 __go_draw_figure__ (h, plot_stream(1), enhanced, mono); 43 __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
44 if (nargin == 5) 44 if (nargin == 5)
45 fid = fopen (debug_file, "wb"); 45 fid = fopen (debug_file, "wb");
46 enhanced = gnuplot_set_term (fid, true, h, term, file); 46 enhanced = gnuplot_set_term (fid, true, h, term, file);
47 __go_draw_figure__ (h, fid, enhanced, mono); 47 __go_draw_figure__ (h, fid, enhanced, mono);
48 endif 48 endif
49 else 49 else
50 error ("__gnuplot_drawnow__: the gnuplot terminal, \"%s\", is not available", 50 error ('__gnuplot_drawnow__: the gnuplot terminal, "%s", is not available',
51 gnuplot_trim_term (term)); 51 gnuplot_trim_term (term));
52 endif 52 endif
53 unwind_protect_cleanup 53 unwind_protect_cleanup
54 set (h, "__plot_stream__", default_plot_stream); 54 set (h, "__plot_stream__", default_plot_stream);
55 if (! isempty (plot_stream)) 55 if (! isempty (plot_stream))
63 endif 63 endif
64 if (! isempty (fid)) 64 if (! isempty (fid))
65 fclose (fid); 65 fclose (fid);
66 endif 66 endif
67 end_unwind_protect 67 end_unwind_protect
68 elseif (nargin == 1) 68 else # nargin == 1
69 ## Graphics terminal for display. 69 ## Graphics terminal for display.
70 plot_stream = get (h, "__plot_stream__"); 70 plot_stream = get (h, "__plot_stream__");
71 if (isempty (plot_stream)) 71 if (isempty (plot_stream))
72 plot_stream = __gnuplot_open_stream__ (2, h); 72 plot_stream = __gnuplot_open_stream__ (2, h);
73 new_stream = true; 73 new_stream = true;
76 endif 76 endif
77 term = gnuplot_default_term (plot_stream); 77 term = gnuplot_default_term (plot_stream);
78 if (strcmp (term, "dumb")) 78 if (strcmp (term, "dumb"))
79 ## popen2 eats stdout of gnuplot, use temporary file instead 79 ## popen2 eats stdout of gnuplot, use temporary file instead
80 dumb_tmp_file = tmpnam (); 80 dumb_tmp_file = tmpnam ();
81 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, ... 81 enhanced = gnuplot_set_term (plot_stream(1), new_stream, h,
82 term, dumb_tmp_file); 82 term, dumb_tmp_file);
83 else 83 else
84 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, term); 84 enhanced = gnuplot_set_term (plot_stream(1), new_stream, h, term);
85 endif 85 endif
86 __go_draw_figure__ (h, plot_stream (1), enhanced, mono); 86 __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
87 fflush (plot_stream (1)); 87 fflush (plot_stream(1));
88 if (strcmp (term, "dumb")) 88 if (strcmp (term, "dumb"))
89 fid = -1; 89 fid = -1;
90 while (fid < 0) 90 while (fid < 0)
91 pause (0.1); 91 pause (0.1);
92 fid = fopen (dumb_tmp_file, 'r'); 92 fid = fopen (dumb_tmp_file, 'r');
93 endwhile 93 endwhile
94 ## reprint the plot on screen 94 ## reprint the plot on screen
95 [a, count] = fscanf (fid, '%c', Inf); 95 [a, count] = fscanf (fid, '%c', Inf);
96 fclose (fid); 96 fclose (fid);
97 if (count>0) 97 if (count > 0)
98 if (a(1)==12) 98 if (a(1) == 12)
99 ## avoid ^L at the beginning 99 a = a(2:end); # avoid ^L at the beginning
100 a = a(2:end);
101 endif 100 endif
102 puts (a); 101 puts (a);
103 endif 102 endif
104 unlink (dumb_tmp_file); 103 unlink (dumb_tmp_file);
105 endif 104 endif
106 else
107 print_usage ();
108 endif 105 endif
109 106
110 endfunction 107 endfunction
111 108
112 function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file) 109 function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file)
115 if (nargin < 4) 112 if (nargin < 4)
116 ## This supports the gnuplot graphics toolkit. 113 ## This supports the gnuplot graphics toolkit.
117 term = gnuplot_default_term (plot_stream); 114 term = gnuplot_default_term (plot_stream);
118 opts_str = ""; 115 opts_str = "";
119 else 116 else
120 ## Get the one word terminal id and save the remaining as options to 117 ## Get the one word terminal id and save the remaining as options to be
121 ## be passed on to gnuplot. The terminal may respect the graphics 118 ## passed on to gnuplot. The terminal may respect the graphics toolkit.
122 ## toolkit.
123 [term, opts_str] = gnuplot_trim_term (term); 119 [term, opts_str] = gnuplot_trim_term (term);
124 term = lower (term); 120 term = lower (term);
125 if (strcmp (term, "lua")) 121 if (strcmp (term, "lua"))
126 ## Replace "lua tikz" with just "tikz" 122 ## Replace "lua tikz" with just "tikz"
127 term = "tikz"; 123 term = "tikz";
147 if (! isempty (h) && isfigure (h)) 143 if (! isempty (h) && isfigure (h))
148 144
149 ## Generate gnuplot title string for plot windows. 145 ## Generate gnuplot title string for plot windows.
150 if (output_to_screen (term) && ! strcmp (term, "dumb")) 146 if (output_to_screen (term) && ! strcmp (term, "dumb"))
151 fig.numbertitle = get (h, "numbertitle"); 147 fig.numbertitle = get (h, "numbertitle");
152 fig.name = strrep (get (h, "name"), "\"", "\\\""); 148 fig.name = strrep (get (h, "name"), '"', '\"');
153 if (strcmp (get (h, "numbertitle"), "on")) 149 if (strcmp (get (h, "numbertitle"), "on"))
154 title_str = sprintf ("Figure %d", h); 150 title_str = sprintf ("Figure %d", h);
155 else 151 else
156 title_str = ""; 152 title_str = "";
157 endif 153 endif
159 title_str = sprintf ("%s: %s", title_str, fig.name); 155 title_str = sprintf ("%s: %s", title_str, fig.name);
160 elseif (! isempty (fig.name) && isempty (title_str)) 156 elseif (! isempty (fig.name) && isempty (title_str))
161 title_str = fig.name; 157 title_str = fig.name;
162 endif 158 endif
163 if (! isempty (title_str)) 159 if (! isempty (title_str))
164 title_str = sprintf ("title \"%s\"", title_str); 160 title_str = sprintf ('title "%s"', title_str);
165 endif 161 endif
166 if (strcmp (term, "aqua")) 162 if (strcmp (term, "aqua"))
167 ## Adjust axes-label and tick-label spacing. 163 ## Adjust axes-label and tick-label spacing.
168 opts_str = sprintf ("%s font \"%s,%d\"", opts_str, 164 opts_str = sprintf ('%s font "%s,%d"', opts_str,
169 get (0, "defaultaxesfontname"), 165 get (0, "defaultaxesfontname"),
170 get (0, "defaultaxesfontsize") / 1.5); 166 get (0, "defaultaxesfontsize") / 1.5);
171 endif 167 endif
172 else 168 else
173 title_str = ""; 169 title_str = "";
193 gnuplot_size = gnuplot_size / 72; 189 gnuplot_size = gnuplot_size / 72;
194 endif 190 endif
195 if (all (gnuplot_size > 0)) 191 if (all (gnuplot_size > 0))
196 terminals_with_size = {"canvas", "emf", "epslatex", "fig", ... 192 terminals_with_size = {"canvas", "emf", "epslatex", "fig", ...
197 "gif", "jpeg", "latex", "pbm", "pdf", ... 193 "gif", "jpeg", "latex", "pbm", "pdf", ...
198 "pdfcairo", "postscript", "png", "pngcairo", ... 194 "pdfcairo", "postscript", "png", ...
199 "pstex", "pslatex", "svg", "tikz"}; 195 "pngcairo", "pstex", "pslatex", "svg", "tikz"};
200 if (__gnuplot_has_feature__ ("windows_figure_position")) 196 if (__gnuplot_has_feature__ ("windows_figure_position"))
201 terminals_with_size{end+1} = "windows"; 197 terminals_with_size{end+1} = "windows";
202 endif 198 endif
203 if (__gnuplot_has_feature__ ("x11_figure_position")) 199 if (__gnuplot_has_feature__ ("x11_figure_position"))
204 terminals_with_size{end+1} = "x11"; 200 terminals_with_size{end+1} = "x11";
205 endif 201 endif
206 if (__gnuplot_has_feature__ ("wxt_figure_size")) 202 if (__gnuplot_has_feature__ ("wxt_figure_size"))
207 terminals_with_size{end+1} = "wxt"; 203 terminals_with_size{end+1} = "wxt";
208 endif 204 endif
209 switch (term) 205 switch (term)
210 case terminals_with_size 206 case terminals_with_size
211 size_str = sprintf ("size %.12g,%.12g", gnuplot_size); 207 size_str = sprintf ("size %.12g,%.12g", gnuplot_size);
212 case "tikz" 208 case "tikz"
213 size_str = sprintf ("size %gin,%gin", gnuplot_size); 209 size_str = sprintf ("size %gin,%gin", gnuplot_size);
214 case "dumb" 210 case "dumb"
215 new_stream = 1; 211 new_stream = 1;
216 if (! isempty (getenv ("COLUMNS")) && ! isempty (getenv ("LINES"))) 212 if (! isempty (getenv ("COLUMNS")) && ! isempty (getenv ("LINES")))
217 ## Let dumb use full text screen size (minus prompt lines). 213 ## Let dumb use full text screen size (minus prompt lines).
218 n = sprintf ("%i", -2 - length (find (sprintf ("%s", PS1) == "\n"))); 214 n = sprintf ("%i", -2 - length (find (sprintf ("%s", PS1) == "\n")));
219 ## n = the number of times \n appears in PS1 215 ## n = the number of times \n appears in PS1
220 size_str = ["size ", getenv("COLUMNS"), ",", getenv("LINES"), n]; 216 size_str = ["size ", getenv("COLUMNS"), ",", getenv("LINES"), n];
221 else 217 else
222 ## Use the gnuplot default. 218 ## Use the gnuplot default.
219 size_str = "";
220 endif
221 case {"aqua", "fig", "corel"}
222 size_str = sprintf ("size %g %g", gnuplot_size);
223 case "dxf"
223 size_str = ""; 224 size_str = "";
224 endif 225 otherwise
225 case {"aqua", "fig", "corel"} 226 size_str = "";
226 size_str = sprintf ("size %g %g", gnuplot_size);
227 case "dxf"
228 size_str = "";
229 otherwise
230 size_str = "";
231 endswitch 227 endswitch
232 if ((strncmpi (term, "x11", 3) 228 if ((strncmpi (term, "x11", 3)
233 && __gnuplot_has_feature__ ("x11_figure_position")) 229 && __gnuplot_has_feature__ ("x11_figure_position"))
234 || (strcmpi (term, "windows") 230 || (strcmpi (term, "windows")
235 && __gnuplot_has_feature__ ("windows_figure_position"))) 231 && __gnuplot_has_feature__ ("windows_figure_position")))
267 title_str = ""; 263 title_str = "";
268 size_str = ""; 264 size_str = "";
269 endif 265 endif
270 266
271 ## Set the gnuplot terminal (type, enhanced, title, options & size). 267 ## Set the gnuplot terminal (type, enhanced, title, options & size).
272 term_str = sprintf ("set terminal %s", term); 268 term_str = ["set terminal " term];
273 if (! isempty (enh_str)) 269 if (! isempty (enh_str))
274 term_str = sprintf ("%s %s", term_str, enh_str); 270 term_str = [term_str " " enh_str];
275 endif 271 endif
276 if (! isempty (title_str)) 272 if (! isempty (title_str))
277 term_str = sprintf ("%s %s", term_str, title_str); 273 term_str = [term_str " " title_str];
278 endif 274 endif
279 if (isempty (strfind (term, "corel"))) 275 if (isempty (strfind (term, "corel")))
280 if (! isempty (size_str) && new_stream) 276 if (! isempty (size_str) && new_stream)
281 ## size_str comes after other options to permit specification of 277 ## size_str comes after other options to permit specification of
282 ## the canvas size for terminals cdr/corel. 278 ## the canvas size for terminals cdr/corel.
283 term_str = sprintf ("%s %s", term_str, size_str); 279 term_str = [term_str " " size_str];
284 endif 280 endif
285 if (nargin > 3 && ischar (opts_str)) 281 if (nargin > 3 && ischar (opts_str))
286 ## Options must go last. 282 ## Options must go last.
287 term_str = sprintf ("%s %s", term_str, opts_str); 283 term_str = [term_str " " opts_str];
288 endif 284 endif
289 else 285 else
290 if (nargin > 3 && ischar (opts_str)) 286 if (nargin > 3 && ischar (opts_str))
291 ## Options must go last. 287 ## Options must go last.
292 term_str = sprintf ("%s %s", term_str, opts_str); 288 term_str = [term_str " " opts_str];
293 endif 289 endif
294 if (! isempty (size_str) && new_stream) 290 if (! isempty (size_str) && new_stream)
295 ## size_str comes after other options to permit specification of 291 ## size_str comes after other options to permit specification of
296 ## the canvas size for terminals cdr/corel. 292 ## the canvas size for terminals cdr/corel.
297 term_str = sprintf ("%s %s", term_str, size_str); 293 term_str = [term_str " " size_str];
298 endif 294 endif
299 endif 295 endif
300 if (! __gnuplot_has_feature__ ("has_termoption_dashed")) 296 if (! __gnuplot_has_feature__ ("has_termoption_dashed"))
301 ## If "set termoption dashed" isn't available add "dashed" option 297 ## If "set termoption dashed" isn't available add "dashed" option
302 ## to the "set terminal ..." command, if it is supported. 298 ## to the "set terminal ..." command, if it is supported.
303 if (any (strcmpi (term, {"aqua", "cgm", "eepic", "emf", "epslatex", \ 299 if (any (strcmpi (term, {"aqua", "cgm", "eepic", "emf", "epslatex", \
304 "fig", "pcl5", "mp", "next", "openstep", "pdf", \ 300 "fig", "pcl5", "mp", "next", "openstep", "pdf", \
305 "pdfcairo", "pngcairo", "postscript", \ 301 "pdfcairo", "pngcairo", "postscript", \
306 "pslatex", "pstext", "svg", "tgif", "x11"}))) 302 "pslatex", "pstext", "svg", "tgif", "x11"})))
307 term_str = sprintf ("%s dashed", term_str); 303 term_str = [term_str " dashed"];
308 endif 304 endif
309 end 305 end
310 306
311 ## Work around the gnuplot feature of growing the x11 window and 307 ## Work around the gnuplot feature of growing the x11 window and
312 ## flickering window (x11, windows, & wxt) when the mouse and 308 ## flickering window (x11, windows, & wxt) when the mouse and
341 endif 337 endif
342 338
343 endfunction 339 endfunction
344 340
345 function term = gnuplot_default_term (plot_stream) 341 function term = gnuplot_default_term (plot_stream)
346 term = getenv ("GNUTERM"); 342 term = lower (getenv ("GNUTERM"));
347 ## If not specified, guess the terminal type. 343 ## If not specified, guess the terminal type.
348 if (isempty (term) || ! __gnuplot_has_terminal__ (term, plot_stream)) 344 if (isempty (term) || ! __gnuplot_has_terminal__ (term, plot_stream))
349 if (isguirunning () && __gnuplot_has_terminal__ ("qt", plot_stream)) 345 if (isguirunning () && __gnuplot_has_terminal__ ("qt", plot_stream))
350 term = "qt"; 346 term = "qt";
351 elseif (ismac ()) 347 elseif (ismac ())
360 endif 356 endif
361 endfunction 357 endfunction
362 358
363 function [term, opts] = gnuplot_trim_term (string) 359 function [term, opts] = gnuplot_trim_term (string)
364 ## Extract the terminal type and terminal options (from print.m) 360 ## Extract the terminal type and terminal options (from print.m)
365 string = deblank (string); 361 string = strtrim (string);
366 n = strfind (string, ' '); 362 [term, opts] = strtok (string, ' ');
367 if (isempty (n)) 363 if (! isempty (opts))
368 term = string; 364 opts(1) = ""; # trim extra space from strtok
369 opts = "";
370 else
371 term = string(1:(n-1));
372 opts = string((n+1):end);
373 endif 365 endif
374 endfunction 366 endfunction
375 367
376 function have_enhanced = gnuplot_is_enhanced_term (plot_stream, term) 368 function have_enhanced = gnuplot_is_enhanced_term (plot_stream, term)
377 persistent enhanced_terminals; 369 ## Don't include pstex, pslatex or epslatex here as the TeX commands
378 if (isempty (enhanced_terminals)) 370 ## should not be interpreted in that case.
379 ## Don't include pstex, pslatex or epslatex here as the TeX commands 371 persistent enhanced_terminals = {"aqua", "canvas", "dumb", "emf", "gif", ...
380 ## should not be interpreted in that case. 372 "jpeg", "pdf", "pdfcairo", "pm", "png", ...
381 enhanced_terminals = {"aqua", "canvas", "dumb", "emf", "gif", "jpeg", ... 373 "pngcairo", "postscript", "qt", "svg", ...
382 "pdf", "pdfcairo", "pm", "png", "pngcairo", ... 374 "windows", "wxt", "x11"};
383 "postscript", "qt", "svg", "windows", "wxt", "x11"}; 375
384 endif
385 if (nargin < 2) 376 if (nargin < 2)
386 ## Determine the default gnuplot terminal. 377 ## Determine the default gnuplot terminal.
387 term = gnuplot_default_term (plot_stream); 378 term = gnuplot_default_term (plot_stream);
388 endif 379 endif
389 have_enhanced = any (strncmp (enhanced_terminals, term, min (numel (term), 3))); 380 have_enhanced = any (strcmp (term, enhanced_terminals));
390 endfunction 381 endfunction
391 382
392 function ret = output_to_screen (term) 383 function ret = output_to_screen (term)
393 ret = any (strcmpi ({"aqua", "dumb", "pm", "qt", "windows", "wxt", "x11"}, term)); 384 ret = any (strcmpi (term,
385 {"aqua", "dumb", "pm", "qt", "windows", "wxt", "x11"}));
394 endfunction 386 endfunction
395 387
396 function retval = have_non_legend_axes (h) 388 function retval = have_non_legend_axes (h)
397 retval = false; 389 retval = false;
398 all_axes = findall (h, "type", "axes"); 390 all_axes = findall (h, "type", "axes");