gnuplot に関する何か他のものを探しているときに、これに出くわしました。古い質問ですが、サンプルコードを提供したいと思いました。私はこれを私のプログラムに使用していますが、かなりきちんとした仕事をしていると思います。私の知る限り、この PIPEing は Unix システムでのみ機能します (Windows ユーザーについては、以下の編集を参照してください)。私の gnuplot インストールは、Ubuntu リポジトリからのデフォルトのインストールです。
#include <stdlib.h>
#include <stdio.h>
#define NUM_POINTS 5
#define NUM_COMMANDS 2
int main()
{
char * commandsForGnuplot[] = {"set title \"TITLEEEEE\"", "plot 'data.temp'"};
double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
FILE * temp = fopen("data.temp", "w");
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); //Write the data to a temporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
}
return 0;
}
編集
私のアプリケーションでは、呼び出し元のプログラムが閉じられるまでプロットが表示されないという問題にも遭遇しました。これを回避するには、最終コマンドの送信に fflush(gnuplotPipe)
使用した後に a を追加します。fprintf
また、Windows ユーザーが--_popen
の代わりに使用する可能性があるpopen
ことも確認しましたが、Windows をインストールしていないため確認できません。
編集2
plot '-'
gnuplot にコマンド、データポイント、文字 "e" を送信することで、ファイルへの書き込みを回避できます。
例えば
fprintf(gnuplotPipe, "plot '-' \n");
int i;
for (int i = 0; i < NUM_POINTS; i++)
{
fprintf(gnuplotPipe, "%lf %lf\n", xvals[i], yvals[i]);
}
fprintf(gnuplotPipe, "e");