WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Gnuplot and Winbatch


Here's how to use gnuplot with WinBatch. First a quick intro to gnuplot and some background...

Gnuplot is a command-line driven program for producing 2D and 3D plots. It is ideal for quickly plotting graphs mathematical functions or data files. It allows you to create images of the plots in various file formats, notably: PS, JPG and PNG.

gnuplot uses a very simple command set to generate plots that can be learned quickly (but it does gets tricky at times). gnuplot has a UI for interactive plotting. For the simplest tasks it takes seconds to type in the commands and for complex tasks you can save the commands to a file. These files can serve as templates for future work. Also it works from the commandline, so it's perfect for automated tasks.

gnuplot has been around on Unix for a long time. It is almost finished a long period of upgrades and version 4 is due out in April. There are very good verions of 3.7.3 and 3.8.x floating around in compiled format. I suggest the latest stable version of 3.7.3 from SourceForge - http://sourceforge.net/projects/gnuplot/.

I use gnuplot in 3 ways in my WinBatch programs...

  1. Feed gnuplot a file full of commands that it understands and have it create me an image in PNG or JPG format. (I usually build the images into HTML documents.)

  2. Using gnuplot interactively via SendKeysTo() commands (in UDFs I created) to let gnuplot display the plot as a separate window. I use this with data collection for all kinds of experiments and it works well.

  3. Create an image (convert it to a BMP with Pixie) and display it in a dialog. (This works, but I don't use it very often. Since I don't mind a second window open I use method #2 instead.)
http://genevalake.ca/winbatch/full_demo.zip

Hope this helps.


Updated GnuPlot Code:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; udf_gnuplot_[yyyymmdd].wbt - UDFs to control 'gnuplot' from WinBatch scripts.
;; 2003/04/16
;; Craig Storey
;;
;; * Tested with gnuplot versions: 3.7, 3.8j, 4.0, 4.1
;;
;; Purpose:  These functions allow you to run an instance of 'gnuplot'
;;   either visible or hidden.  The functions use Windows ID handles to
;;   keep track of the applications thus any number of 'gnuplot' applications
;;   may be run and controlled at one time to produce multiple plots.  The
;;   functions allow 'gnuplot' commands to be sent to any individual 'gnuplot'
;;   application for which a handle exists.  Repositioning and resizing
;;   are also supported.
;;
;; Functions:
;;   - udf_findgnuplot(dirstart)
;;   - udf_startgnuplot(gpexe_location, hide)
;;   - udf_sendplotstr2gnuplot(gp_winid, command, plot_title, placement)
;;   - udf_sendstr2gnuplot(gp_winid, command)
;;   - udf_movegnuplot(gpplot_winid, placement)
;;   - udf_placegnuplot(gpplot_winid, x_ulc, y_ulc, x_brc, y_brc)
;;   - udf_endgnuplot(gp_winid)
;;
;; Modifications:
;;   - SendKeysTo() fixes for Windows 95 computers. - 2004/04/29
;;   - udf_findgnuplot() - 2004/05/04
;;   - add_sendkeys_pause, which adds a delay for Win95 (old+slow) machines to allow gnuplot processing time - 2004/05/18
;;   - IgnoreInput(@ON) .. SendKeysTo() .. IgnoreInput(@OFF) - 2004/06/08
;;   - ErrorMode(@OFF).. WinActivate().. ErrorMode(@ON) - 2004/06/08
;;   - Added udf_replaceCharForSendKeys(my_sendkey_string) to fix special SendKeys Chars - 2005/11/01
;;
;; To Do:
;;   - paly with control manager Extender functions instead of SendKeysTo.
;;   - play with pipes (gnuplot_pipes.exe/pgnuplot.exe) instead of SendKeysTo.
;;   - Create a gnuplot WinBatch extender from available C libraries.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;; Speed up SendkeysTo or Slow them Down!
;; Windows 95 Labcomputer needs to slow down SedKeysTo(), other OS's don't need this enabled!
v = WinVersion(5)
If v=="1-4-0" ; Windows 95
   IntControl(35,10,0,0,0)  ;10 milisecond delay between keys on Win95
   add_sendkeys_pause=@TRUE
Else
   IntControl (35, 0, 0, 0, 0)   ; General speedup
   IntControl (45, 1, 0, 0, 0)   ; DOS/WinNT speed up
   add_sendkeys_pause=@FALSE
EndIf

;;;; For testing you may want to slow down SendKeysTo..
;IntControl (35, 100, 0, 0, 0) ; 100 milisecond delay

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; gnuplot is a portable EXE and may be installed anywhere.
; Thus on some machines you may need to do a search to find it...
; This UDF will find it if it's on the HD you think it's on.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_replaceCharForSendKeys(my_sendkey_string)
   ; Special SendKeys Strings...
   ;  ~    {~}       This is how to send a ~
   ;  !    {!}       This is how to send a !
   ;  ^    {^}       This is how to send a ^
   ;  +    {+}       This is how to send a +
   ;  {    { { }     This is how to send a {
   ;  }    { } }     This is how to send a }
   ;  Enter   {ENTER} or ~
   ;  Space   {SPACE} or {SP}
   ;  Tab     {TAB}

   ; This needs to be reworked, the "{" converting doesn't work properly in all cases, like nesting.
   ;   my_sendkey_string = StrReplace(my_sendkey_string, "{", "{{}")
   ;   my_sendkey_string = StrReplace(my_sendkey_string, "}", "{}}")

   my_sendkey_string = StrReplace(my_sendkey_string, "~", "{~}")
   my_sendkey_string = StrReplace(my_sendkey_string, "!", "{!}")
   my_sendkey_string = StrReplace(my_sendkey_string, "^", "{^}")
   my_sendkey_string = StrReplace(my_sendkey_string, "+", "{+}")

   my_sendkey_string = StrReplace(my_sendkey_string, @CRLF, "{ENTER}")

   Message("my_sendkey_string", my_sendkey_string)

   Return (my_sendkey_string)
#EndFunction



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; gnuplot is a portable EXE and may be installed anywhere.
; Thus on some machines you may need to do a search to find it...
; This UDF will find it if it's on the HD you think it's on.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_findgnuplot(dirstart)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
   ; gnuplot is likely in... C:\Program Files\gp38jw32\binaries\wgnuplot.exe
   ; but maybe anywhere on the machine.
   ; This should find it.
   lookfor="wgnuplot.exe"
   MyFullFileName=udf_FileFind(dirstart,lookfor)
   If MyFullFileName==""
      Return
   EndIf
   Return(MyFullFileName)
#EndFunction


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Recursive File Find routine.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_FileFind(dir,DaFile)
   origdir=DirGet()
   DirChange(dir)
   If FileExist(dafile)
      retval=StrCat(DirGet(),DaFile)
   Else
      dirlist=DirItemize("*.*")
      dircount=ItemCount(dirlist,@TAB)
      retval=""
      For xx=1 To dircount
         thisdir=ItemExtract(xx,dirlist,@TAB)
         retval=udf_FileFind(thisdir,DaFile)
         If retval!="" Then Break
      Next
   EndIf
   DirChange(origdir)
   Return(retval)
#EndFunction


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Start gnuplot in hidden mode, create unique title for each instance of gnuplot,
;  get the Windows ID and return it for future commands to use.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_startgnuplot(gpexe_location, hide)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
   If hide==1
      RunHide(gpexe_location, "")
   Else
      Run(gpexe_location, "")
   EndIf
   a=Random(999)
   b=Random(999)
   rdn_title=StrCat(a,"#gnuplot#",b)
   WinTitle("gnuplot", rdn_title)
   gp_winid = WinIdGet(rdn_title)
   Return (gp_winid)
#EndFunction


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Send a gnuplot command string (that generates a plot) to gnuplot, CRLF will be added.
; This UDF should be used only the first time a graph is generated.
; Place the graph someplace else if it is created exists.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_sendplotstr2gnuplot(gp_winid, my_command, plot_title, placement)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))

   ErrorMode(@OFF)
   WinActivate(gp_winid)
   ErrorMode(@ON)

   IgnoreInput(@ON)
   ;my_command = udf_replaceCharForSendKeys(my_command)
   SendKeysTo(gp_winid, StrCat(my_command, @CRLF))
   IgnoreInput(@OFF)

;   If add_sendkeys_pause==@TRUE then TimeDelay(0.2)
;   SendKeysTo(gp_winid, StrCat("reset; unset mouse", @CRLF)) ; Turn off the mouse-over cursor.

   If WinExist("~graph")==@TRUE
      WinTitle("~graph", plot_title)
      If placement==1 Then WinPlace(0, 0, 500, 500, plot_title)          ;Top Left
      If placement==2 Then WinPlace(500, 0, 1000, 500, plot_title)       ;Top Right
      If placement==3 Then WinPlace(500, 250, 1000, 750, plot_title)     ;Middle Right
      If placement==4 Then WinPlace(0, 250, 500, 750, plot_title)        ;Middle Left
      If placement==5 Then WinPlace(0, 500, 500, 1000, plot_title)       ;Bottom Left
      If placement==6 Then WinPlace(500, 500, 1000, 1000, plot_title)    ;Bottom Right
      If placement==7 Then WinPlace(250, 250, 750, 750, plot_title)      ;Center
      If placement==9 Then WinPlace(0, 0, 1000, 1000, plot_title)        ;Zoomed!
      gpplot_winid=WinIdGet(plot_title)
      Return(gpplot_winid)
   EndIf
   Return
#EndFunction

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Send a gnuplot command string to gnuplot, CRLF will be added.
; The Graph will not be moved with htis function.
;
; ** THERE IS SOMETIMES AN ERROR HERE THAT NEEDS LOOKING INTO!
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_sendstr2gnuplot(gp_winid, my_command)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
;   If add_sendkeys_pause==@TRUE then TimeDelay(0.2)
   ErrorMode(@OFF)
   WinActivate(gp_winid)
   ErrorMode(@ON)

   IgnoreInput(@ON)
   ;my_command = udf_replaceCharForSendKeys(my_command)
   SendKeysTo(gp_winid, StrCat(my_command, @CRLF))
   IgnoreInput(@OFF)

   Return
#EndFunction

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Move the graph to 1 of 7 locations on the screen
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_movegnuplot(gpplot_winid, placement)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
   ErrorMode(@OFF)
   WinActivate(gpplot_winid)
   ErrorMode(@ON)
   Switch placement
      Case 1
         WinPlace(0, 0, 500, 500, gpplot_winid)          ;Top Left
      Break
      Case 2
         WinPlace(500, 0, 1000, 500,  gpplot_winid)      ;Top Right
      Break
      Case 3
         WinPlace(500, 250, 1000, 750, gpplot_winid)     ;Middle Right
      Break
      Case 4
         WinPlace(0, 250, 500, 750,  gpplot_winid)       ;Middle Left
      Break
      Case 5
         WinPlace(0, 500, 500, 1000,  gpplot_winid)      ;Bottom Left
      Break
      Case 6
         WinPlace(500, 500, 1000, 1000,  gpplot_winid)   ;Bottom Right
      Break
      Case 7
         WinPlace(250, 250, 750, 750,  gpplot_winid)     ;Center
      Break
   EndSwitch
   Return
#EndFunction


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Full control to Position/Resize the plot in any manner
;
; Parameters:
;  (s) gpplot_winid - Handle that identified the gnuplot graph
;  (i) x-ulc - How far from the left of the screen to place
;      the upper-left corner (0-1000).
;   i) y-ulc - How far from the top of the screen to place
;      the upper-left corner (0-1000).
;  (i) x-brc - How far from the left of the screen to place
;      the bottom-right corner (10-1000) or @NORESIZE.
;  (i) y-brc - How far from the top of the screen to place
;      the bottom-right corner (10-1000) or @NORESIZE.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_placegnuplot(gpplot_winid, x_ulc, y_ulc, x_brc, y_brc)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
   ErrorMode(@OFF)
   WinActivate(gpplot_winid)
   ErrorMode(@ON)
   WinPlace(x_ulc, y_ulc, x_brc, y_brc, gpplot_winid)
   Return
#EndFunction


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; End gnuplot sesssion & delete the plot if passed a gnuplot WinID
; Delete only the plot if passed a gnuplot plot WinID
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udf_endgnuplot(gp_winid)
;DebugTrace(@ON, StrCat(FilePath(IntControl(1004,0,0,0,0)), "debug_udf_gnuplot.txt"))
   WinClose(gp_winid)
   Return
#EndFunction



;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Examples....
;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Usually install gnuplot at "C:\Program Files\gp400win32\bin\wgnuplot.exe",
;; But just in case it's best to find it...
;gp_location1=udf_findgnuplot("c:\")
;gp_location2=gp_location1
;
;winida=udf_startgnuplot(gp_location1, 1)    ; Don't hide this instance
;winidb=udf_startgnuplot(gp_location2, 1)   ; Hide this one
;
;Display(2, 1, "Test: Create Plots and Re-name")
;gpplot_winida = udf_sendplotstr2gnuplot(winida, "plot x with lines 0, -x with lines 0", "Graph A", 0)
;gpplot_winidb = udf_sendplotstr2gnuplot(winidb, "test", "Graph A", 13)   ; Create a second (centered) plot same name (Note no conflicts)
;
;Display(2, 2, "Test: Move gnuplot plot windows")
;udf_movegnuplot(gpplot_winida, 2)                  ; Move the plot to upper 1/2 right 1/2
;udf_movegnuplot(gpplot_winidb, 1)                  ; Move to upper 1/2 left 1/2
;TimeDelay(.5)
;udf_movegnuplot(gpplot_winida, 3)                  ; Move to mid 1/2 right 1/2
;udf_movegnuplot(gpplot_winidb, 5)                  ; Move to lower 1/2 right 1/2
;
;Display(2, 3, "Test: Additional Strings to gnuplot")
;udf_sendstr2gnuplot(winidb, "plot sin(x),2*cos(x/2)")      ; Send new plot instructions
;udf_sendstr2gnuplot(winida, "set size .8,.4; set multiplot; set origin .05,.05;pl sin(x); set origin .05,.55; pl cos(x);set nomultiplot") ; Send multiplot instructions
;
;Display(2, 4, "Test: Placing gnuplot plots anywhere")
;udf_placegnuplot(gpplot_winida, 0,0, 1000, 250)            ; Change size and position
;udf_placegnuplot(gpplot_winidb, 0, 250, 1000, 1000)      ; Ditto
;
;;Display(2, 5, "Plot from a file")
;;udf_sendstr2gnuplot(winidb, "plot 'wbt_gnuplot.gpl'")
;
;Message(1, "Ok, to Exit")
;udf_endgnuplot(winida)
;udf_endgnuplot(winidb)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Article ID:   W16695
File Created: 2005:12:01:15:40:00
Last Updated: 2005:12:01:15:40:00