☆コンテンツ
下記のgnuplotによるコマンドをC++(VisualStudio)にて実行する。
set xrange [-10:10] set yrange [-1:1] plot sin(x)
★C++コード
#include <stdio.h> #include <stdlib.h> #define GNUPLOT_PATH "C:/PROGRA~1/gnuplot/bin/gnuplot.exe" // gnuplot.exeのある場所を記載。 // \ではなく、/を使う。 // Program FilesはPROGRA~1、Program Files(x86)はPROGRA~2に置き換える int main() { FILE* gp; // For gnuplot // gnuplotの起動コマンド if ((gp = _popen(GNUPLOT_PATH, "w")) == NULL) { // gnuplotをパイプで起動 fprintf(stderr, "ファイルが見つかりません %s.", GNUPLOT_PATH); exit(EXIT_FAILURE); } // ↓↓--- gnuplotコマンド開始 ---↓↓ // fprintf(gp, "set xrange [-10:10]\n"); fprintf(gp, "set yrange [-1:1]\n"); fprintf(gp, "plot sin(x)\n"); // ↑↑--- gnuplotコマンド終了 ---↑↑ // fflush(gp); // バッファに格納されているデータを吐き出す(必須) system("pause"); fprintf(gp, "exit\n"); // gnuplotの終了 _pclose(gp); }
★実行結果
gnuplotによる、sin(x)のグラフが表示される。
★解説
・gnuplot.exeのある場所の記載でエラーが起こりやすい。
・gnuplotコマンドの部分の\nは忘れない。
★参考
http://www.eng.kagawa-u.ac.jp/~haruna/memo/gnuplot/gnuC.html