1### Copyright (c) 2007, Tyzx Corporation. All rights reserved. 2 3function g = g_plot (g, varargin) 4### g = g_plot (g,...) - Plot a gnuplot_object 5### 6### If g.values has a field "geometry" of the form, [width, heigth] or 7### "<width>x<height>, it will be used to set the geometry, in pixels of the 8### gnuplot window. 9### 10### 11### 12### OPTIONS: 13### "geometry",geometry 14### 15### "-wait" : Gnuplot does not exit immediately, which allows to manipulate the 16### plot with the mouse. 17### 18### Any string ending in '.eps' causes postscript output. The plot is output to 19### that file, and the file is viewed using ggv. 20### 21### "-display", yesno : View eps file or not (senseless unless an eps was saved) 22## 23### "-color", yesno : In color or monochrome (for eps and fig) 24### 25### All optional arguments other than the options above will be passed to 26### _g_instantiate before plotting. 27### 28### TODO: Have a better choice of postscript viewers (e.g. gv ...) 29### Do the same for .pdf, .fig and .png arguments. 30### 31### See also: g_new,... 32 33 _g_check (g); 34 35 do_eps = 0; 36 do_fig = 0; 37 do_png = 0; 38# do_ppm = 0; 39# do_pgm = 0; 40# do_jpg = 0; 41 do_display = 1; 42 outputFile = ""; 43 gnuplot_options = ""; 44 geometry = []; 45 pre_cmd = {}; 46 pos_cmd = {}; 47 do_color = 1; 48 49 wait_for_q = struct_contains (g.values, "wait") && g.values.wait; 50 51 if 0 52 eps_viewer = "ggv"; 53 else 54 eps_viewer = "gv"; 55 end 56 if 0 57 png_viewer = "qiv"; 58 elseif 0 59 png_viewer = "xzgv"; 60 elseif 0 61 png_viewer = "display"; 62 else 63 png_viewer = "eog"; 64 end 65 66 i = 1; 67 keep = ones (1,length(varargin)); # Opts that'll be passed to _g_instantiate 68 while i <= length (varargin) 69 opt = varargin{i++}; 70 ll = length(opt); 71 if ll>=4 \ 72 && (strcmp (opt(ll-3:ll), ".eps") \ 73 || strcmp (opt(ll-3:ll), ".fig") \ 74 || strcmp (opt(ll-3:ll), ".png")) 75 76 if do_eps || do_fig || do_png, 77 printf ("Will save to '%s'\n",opt); 78 end 79 switch opt(ll-3:ll) 80 case ".eps", do_eps = 1; 81 case ".fig", do_fig = 1; 82 case ".png", do_png = 1; 83 endswitch 84 85 outputFile = opt; 86 keep(i-1) = 0; 87 88 elseif strcmp (opt, "-wait") 89 wait_for_q = 1; 90 keep(i-1) = 0; 91 92 elseif strcmp (opt, "geometry") 93 94 geometry = varargin{i++}; 95 keep([i-2,i-1]) = 0; 96 97 elseif strcmp (opt, "-display") 98 if i > length (varargin) \ 99 || (i == length(varargin) && !isnumeric (varargin{i})) 100 do_display = 1; 101 keep([i-1]) = 0; 102 else 103 do_display = varargin{i++}; 104 keep([i-2,i-1]) = 0; 105 endif 106 elseif strcmp (opt, "-color") 107 if i > length (varargin) \ 108 || (i == length(varargin) && !isnumeric (varargin{i})) 109 do_color = 1; 110 keep([i-1]) = 0; 111 else 112 do_color = varargin{i++}; 113 keep([i-2,i-1]) = 0; 114 endif 115 else 116 ##error ("Unknown option '%s'",opt); 117 endif 118 endwhile # EOF reading options 119 120 # Make outputFile absolute 121 if length(outputFile) && outputFile(1) != "/" 122 outputFile = [pwd(),"/",outputFile]; 123 endif 124 125 if do_eps 126 if isempty (geometry) 127 if ! struct_contains (g.values, "geometry") 128 geometry = [10,8]; 129 else 130 geometry = g.values.geometry; 131 if ischar(geometry) 132 geometry = strrep (geometry, "x", " "); 133 geometry = eval (["[",geometry,"]"]) 134 endif 135 geometry ./= 28; 136 endif 137 endif 138 elseif do_png 139 if isempty (geometry) 140 if ! struct_contains (g.values, "geometry") 141 geometry = [600,400]; 142 else 143 geometry = g.values.geometry; 144 if ischar(geometry) 145 geometry = strrep (geometry, "x", " "); 146 geometry = eval (["[",geometry,"]"]) 147 endif 148 ##geometry .*= 72; 149 endif 150 endif 151 endif 152 if do_color, colorStr = "color"; else colorStr = "monochrome"; end 153 if do_eps 154 155 pre_cmd = {sprintf("set term postscript eps %s size %.1fcm,%.1fcm 20",colorStr,geometry),\ 156 ["set out '",outputFile,"'"]}; 157 158 elseif do_fig 159 pre_cmd = {sprintf("set term fig big landscape metric %s fontsize 20 ",colorStr),\ 160 ["set out '",outputFile,"'"]}; 161 162 elseif do_png 163 164 if do_color 165 pcolors = "xffffff x000000 x000000 xc00000 xa0ffa0 x0000ff xE0E000 x00E0E0 xE000E0 x000000"; 166 else 167 pcolors = "xffffff x000000 x000000 x000000 x808080 xA0A0A0 xE0E0E0 x404040 xC0C0C0 x202020"; 168 end 169 pre_cmd = {sprintf("set term png truecolor size %i, %i %s",geometry, pcolors),\ 170 ["set out '",outputFile,"'"]}; 171 172 elseif wait_for_q # Not eps and -wait 173 174 pre_cmd = {pre_cmd{:},\ 175 "set mouse",\ 176 "set mouse labels",\ 177 "set mouse verbose"\ 178 }; 179 180 pos_cmd = {pos_cmd{:},\ 181 "pause -1"}; 182 183 ## FIXME: gnuplot croaks w/ warning: Mousing not active, even if I've set mouse.. 184 ## "pause mouse keypress"}; 185 186 ## This hack does not work either 187 #,\ "if (MOUSE_KEY != 113) reread"}; 188 189 else # Neither eps nor -wait 190 gnuplot_options = " -persist "; 191 endif 192 g = _g_instantiate (g,varargin{find(keep)}); 193 194 195 cmd_str = sprintf ("%s\n",pre_cmd{:},g.cmds{:},pos_cmd{:}); 196 197 ## Redundant w/ geometry treatment above? 198 if !isempty (geometry) 199 if !ischar (geometry), 200 opt_str = sprintf ("%ix%i",geometry); 201 else 202 opt_str = geometry; 203 endif 204 gnuplot_options = [gnuplot_options," -geometry ",opt_str," "]; 205 206 elseif struct_contains (g.values, "geometry") 207 if !ischar (g.values.geometry), 208 opt_str = sprintf ("%ix%i",g.values.geometry); 209 else 210 opt_str = g.values.geometry; 211 endif 212 gnuplot_options = [gnuplot_options," -geometry ",opt_str," "]; 213 endif 214 215 ## Hack for png font 216 if do_png, cmd_str = strrep (cmd_str, "Times-Roman",""); endif 217 cmdfname = [g.dir,"/cmds.plt"]; 218 [fid, msg] = fopen (cmdfname,"w"); 219 if fid<0, 220 error ("Can't open command file '%s': %s", cmdfname, msg); 221 endif 222 fprintf (fid,"cd '%s'\n",g.dir); 223 fprintf (fid, "%s", cmd_str); 224 fclose (fid); 225 226 ##system (["cat ",cmdfname]); 227 gnuplot_command = [g_config("gnuplot_program")," ",gnuplot_options,\ 228 " ",cmdfname]; 229 230 output_file = [g.dir,"/gnuplot-output.txt"]; 231 gnuplot_command = [gnuplot_command, " 2>&1 | tee ",output_file]; 232 233 status = system (gnuplot_command); 234 [status2, msg] = system (["cat ",output_file]); 235 236 if !status 237 238 tokens = regexp (msg, 'put\D*([\+\-\d\.]+)\D+([\+\-\d\.]+)', "tokens"); 239 if length (tokens) 240 double_clicks = zeros (length (tokens),2); 241 for i=1:length (tokens) 242 # Strangely eval seems faster than str2double 243 double_clicks(i,1) = eval (tokens{i}{1}); 244 double_clicks(i,2) = eval (tokens{i}{2}); 245 end 246 ##if struct_contains (g, "double_clicks") 247 ##g.double_clicks = [g.double_clicks; double_clicks]; 248 ##else 249 ## Keep only clicks from last plot 250 g.double_clicks = double_clicks; 251 ##end 252 end 253 254 tokens = regexp (msg, 'set label\D*([\+\-\d\.]+)\D+([\+\-\d\.]+)', "tokens"); 255 if length (tokens) 256 middle_clicks = zeros (length (tokens),2); 257 for i=1:length (tokens) 258 # Strangely eval seems faster than str2double 259 middle_clicks(i,1) = eval (tokens{i}{1}); 260 middle_clicks(i,2) = eval (tokens{i}{2}); 261 end 262 ##if struct_contains (g, "middle_clicks") 263 ##g.middle_clicks = [g.middle_clicks; middle_clicks]; 264 ##else 265 ## Keep only clicks from last plot 266 g.middle_clicks = middle_clicks; 267 ##end 268 end 269 270 else 271 printf ("Couldn't run gnuplot: Exited, saying \n%s\n",msg); 272 #keyboard 273 endif 274 275 if do_display 276 if do_eps 277 [status, msg] = system ([g_config("eps_viewer")," ",outputFile]); 278 if status 279 error ("Couldn't run %s: Exited, saying \n%s\n",eps_viewer, msg); 280 endif 281 elseif do_fig 282 [status, msg] = system (["xfig ",outputFile]); 283 if status 284 error ("Couldn't run xfig: Exited, saying \n%s\n",msg); 285 endif 286 elseif do_png 287 [status, msg] = system ([g_config("png_viewer")," ",outputFile]); 288 if status 289 error ("Couldn't run %s: Exited, saying \n%s\n",png_viewer, msg); 290 endif 291 endif 292 endif 293 ##keyboard 294 if nargout<1, clear g; endif 295endfunction 296 297