2010-12-15 87 views

回答

4

gnuplot的支持通過管道輸入(在Windows中,有一個爲這一個單獨的可執行,pgnuplot)。然後你的程序可以向gnuplot發送新命令,例如replot,就像直接將它們輸入到gnuplot接口一樣。

你如何建立管道連接,並寫入管道從C發送端++程序因操作系統而異,所以你必須告訴我們您正在使用的是什麼,如果你想更多的幫助。

在Windows上,有CreatePipe,然後將STARTUPINFO結構的hStdInput元素設置爲CreateProcess。如果您需要pgnuplot的狀態消息,請與hStdOutput同上。

在POSIX(Unix,Linux,Mac OSX等)上,您可以使用popen作爲獲得單向連接的快捷方式。對於雙向,它在Windows上更像:pipe獲取句柄的結尾,然後fork,並在子進程調用dup2將stdin和stdout與管道關聯,然後execgnuplot替換子進程,保持管道建立。

編輯:從the gnuplot documentation

的特殊文件名「 - 」指定 該數據是內聯;即,他們按照該命令 。只有數據 遵循命令; 陰謀選項如 過濾器,標題和線型 仍然在陰謀命令行。這 類似於<在UNIX < shell腳本, 和VMS DCL $甲板。這些數據是 進入,就好像他們正在讀 從一個文件,每 記錄一個數據點。第一列 開頭的字母「e」終止數據 條目。 使用選項可以應用 這些數據 - 使用它來過濾 他們通過一個函數可能會使 感,但選擇列可能 不!

+0

我認爲,但發送個別點的命令是什麼?我只知道如何告訴它繪製數據文件或函數。我該如何繪製像x1 y1,x2 y2,逐點? – chutsu 2010-12-15 12:15:34

+0

@chutsu:我沒有提供這些細節,因爲你說你已經知道gnuplot的一部分。無論如何,從文檔中添加相關段落。 – 2010-12-15 17:47:40

1

如果您對軟實時繪圖感興趣,您可能最好使用硬件加速圖形API(如OpenGL),並自行繪製圖表。

+0

我真的很希望能利用gnuplot的,原因是什麼是否準備好知道,我已經看到了使用的Perl流的真實數據爲gnuplot的各種接口,而是因爲這是我的單場的分配,課程總監確實希望我用C++來完成。 – chutsu 2010-12-15 00:56:14

+1

定義「實時」對你的意義。如果它的意思是「每秒更新25次」,那麼gnuplot可能不適合你。 – etarion 2010-12-15 01:04:32

+0

對我來說,第一個陰謀是很大的。 – chutsu 2010-12-15 12:18:38

0

在我的C++代碼,這個工作(在Mac OSX上特立獨行,採用G ++蘋果LLVM 5版本。0):

#include <sys/types.h> 
#include <unistd.h> 

... 

// ready to make a plot 

pid_t childpid=fork(); 
if(childpid==0) { 
    // child process makes plot 
    std::FILE* pipehandle=popen("gnuplot -persist","w"); 
    // make some plot. You can send multiple commands to the pipe each ending in \n 
    std::fprintf(pipehandle,"plot \"results.txt\" using 1:2 with lines\n"); 
    std::fprintf(pipehandle,"quit\n"); 
    std::fflush(pipehandle); 
    std::fclose(pipehandle); 
    // child process exits 
    exit(0); 
} 
// parent process waits for child process to exit 
waitpid(childpid,NULL,0); 

// you can now repeat to make other gnuplots; all will appear simultaneously in the 
// terminal and are persistent after the parent process has finished.