2012-02-19 167 views
4

我有以下內容,無論我嘗試命令窗口是否打開並再次關閉。沒有繪圖,沒有文件被寫入。任何有解決方案的人都可以使用C++的gnuplot。我有4.4和4.6rc1可用。Gnuplot,來自windows的C++。命令窗口打開和關閉

#ifdef WIN32 
    gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w"); 
#else 
    gp = popen("gnuplot -persist", "w"); 
#endif 


if (gp == NULL) 
     return -1; 

    /* fprintf(gp, "unset border\n"); 
    fprintf(gp, "set clip\n"); 
    fprintf(gp, "set polar\n"); 
    fprintf(gp, "set xtics axis nomirror\n"); 
    fprintf(gp, "set ytics axis nomirror\n"); 
    fprintf(gp, "unset rtics\n"); 
    fprintf(gp, "set samples 160\n"); 
    fprintf(gp, "set zeroaxis"); 
    fprintf(gp, " set trange [0:2*pi]");*/ 


    fprintf(gp, "set term png\n"); 
    fprintf(gp, "set output \"c:\\printme.png\""); 
    fprintf(gp, "plot .5,1,1.5\n"); 
    fprintf(gp, "pause -1\n"); 

    fflush(gp); 
+0

作品只是罰款的Visual Studio 2013? – JRG 2012-02-20 17:00:43

+0

現在這樣做,但真的很喜歡從我的應用程序向用戶顯示直方圖。 – 2012-02-20 17:53:45

回答

5

下面的程序在Windows上使用Visual Studio和MinGW編譯器進行了測試,以及GNU/Linux上使用gcc。必須使用gnuplot二進制文件,並且在Windows上,必須使用管道版本的二進制文件。

我發現Windows管道比GNU/Linux上相應的管道慢得多。對於大型數據集,通過Windows上的管道將數據傳輸到gnuplot的速度很慢並且通常不可靠。此外,按鍵等待代碼在GNU/Linux上更有用,其中一旦調用pclose(),繪圖窗口將關閉。

#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 

// Tested on: 
// 1. Visual Studio 2012 on Windows 
// 2. Mingw gcc 4.7.1 on Windows 
// 3. gcc 4.6.3 on GNU/Linux 

// Note that gnuplot binary must be on the path 
// and on Windows we need to use the piped version of gnuplot 
#ifdef WIN32 
    #define GNUPLOT_NAME "pgnuplot -persist" 
#else 
    #define GNUPLOT_NAME "gnuplot" 
#endif 

int main() 
{ 
    #ifdef WIN32 
     FILE *pipe = _popen(GNUPLOT_NAME, "w"); 
    #else 
     FILE *pipe = popen(GNUPLOT_NAME, "w"); 
    #endif 

    if (pipe != NULL) 
    { 
     fprintf(pipe, "set term wx\n");   // set the terminal 
     fprintf(pipe, "plot '-' with lines\n"); // plot type 
     for(int i = 0; i < 10; i++)    // loop over the data [0,...,9] 
      fprintf(pipe, "%d\n", i);   // data terminated with \n 
     fprintf(pipe, "%s\n", "e");    // termination character 
     fflush(pipe);       // flush the pipe 

     // wait for key press 
     std::cin.clear(); 
     std::cin.ignore(std::cin.rdbuf()->in_avail()); 
     std::cin.get(); 

     #ifdef WIN32 
       _pclose(pipe); 
     #else 
       pclose(pipe); 
     #endif 
    } 
    else 
     std::cout << "Could not open pipe" << std::endl; 
return 0; 
} 
+0

bin文件夾中沒有'pgnuplot'。它應該分開安裝嗎? – locke14 2015-04-30 13:41:17

+0

改爲嘗試使用wgnuplot_pipes.exe。基本上,應該有一個支持Windows上的管道的gnuplot版本。 – 2015-04-30 16:53:16

+0

我在5.0.0版本中找不到任何管道版本的gnuplot。但是,版本4.6.6中有'pgnuplot'。版本5.0的 – locke14 2015-05-04 14:32:51

1

當然,以下的答案頗爲相似尼古拉斯Kinar的答案,只增加一點就是如何正確保存.png文件。還有一些建議:

  1. 如果輸入路徑有空格,它將不起作用。在使用Visual Studio 2013的Windows 8中,它顯示錯誤"C:Program" is not recognized as an internal or external command....
  2. 如果您沒有提及任何輸出路徑,它將打印並保存在C++程序本身的目錄中。所以你必須檢查輸出路徑的正確格式,因爲gnuplot沒有顯示任何錯誤,所以很難找出確切的原因。

下面是完整的C++程序,爲什麼不寫你的命令文件,然後使用該腳本運行的gnuplot在Windows 8.1

#include <cstdio> 
#include <iostream> 
int main() 
{ 

    FILE* pipe = _popen("C:/gnuplot/bin/pgnuplot.exe", "w"); 
    if (pipe != NULL) 
    { 
     fprintf(pipe, "set term win\n"); 
     fprintf(pipe, "plot(x, sin(x))\n"); //a simple example function 
     fprintf(pipe, "set term pngcairo\n"); 
     fprintf(pipe, "set output \"myFile.png\"\n"); 
     fprintf(pipe, "replot\n"); 
     fprintf(pipe, "set term win\n"); 
     fflush(pipe); 
    } 
    else puts("Could not open the file\n"); 
    _pclose(pipe); 
    //system("pause"); 
    return 0; 
}