2016-12-01 8 views
0

私のプログラムを実行すると、私のプログラムはグラフをプロットして画面に表示したい。しかし、私はそれをどうすればいいのか分かりません。 C++コードの例を示します。C++でGNUplotを使ってグラフをプロットする方法

#include "stdafx.h" 
# include <cstdlib> 
# include <iostream> 
# include <iomanip> 
# include <cmath> 

using namespace std; 

# include "curve_plot.h" 

int main(); 
int main() 


{ 
    int i; 
    int n; 
    double *x; 
    double *y; 

    cout << "\n"; 
    cout << "CURVE_PLOT_PRB:\n"; 
    cout << " Demonstrate how CURVE_PLOT can be used.\n"; 

    // Set up some data to plot. 

    n = 51; 
    x = new double[n]; 
    y = new double[n]; 
    for (i = 0; i < 51; i++) 
    { 
     x[i] = (double)(i)/10.0; 
     y[i] = x[i] * cos(x[i]); 
    } 

    // Send the data to curve_plot. 
    curve_plot(n, x, y, "curve_plot"); 

    // Free memory. 


    delete[] x; 
    delete[] y; 



    return 0; 
} 

ここにヘッダーファイルがあります。

void curve_plot(int n、double x []、double y []、string name);

#include "stdafx.h" 
# include <cstdlib> 
# include <iostream> 
# include <iomanip> 
# include <fstream> 
#include <string> 
using namespace std; 

# include "curve_plot.h" 

void curve_plot(int n, double x[], double y[], string name) 


{ 
    string command_filename; 
    ofstream command_unit; 
    string data_filename; 
    ofstream data_unit; 
    int i; 
    string plot_filename; 

    // Write the data file. 

    data_filename = name + "_data.txt"; 
    data_unit.open(data_filename.c_str()); 
    for (i = 0; i < n; i++) 
    { 
     data_unit << x[i] << " " 
      << y[i] << "\n"; 
    } 
    data_unit.close(); 
    cout << "\n"; 
    cout << " Plot data written to the file \"" << data_filename << "\".\n"; 

    // Write the command file. 

    command_filename = name + "_commands.txt"; 
    command_unit.open(command_filename.c_str()); 
    command_unit << "set term png\n"; 
    plot_filename = name + ".png"; 
    command_unit << "set output \"" << plot_filename << "\"\n"; 
    command_unit << "set grid\n"; 
    command_unit << "set style data lines\n"; 
    command_unit << "unset key\n"; 
    command_unit << "set xlabel '<---X--->'\n"; 
    command_unit << "set ylabel '<---Y--->'\n"; 
    command_unit << "set timestamp\n"; 
    command_unit << "plot \"" << data_filename << "\" using 1:2 with lines lw 3\n"; 
    command_unit << "quit\n"; 
    command_unit.close(); 
    cout << " Command data written to \"" << command_filename << "\".\n"; 

    return; 
} 

答えて

0

は、あなたが何をしたいですかcurve_plot.cpp、あなたのC++プログラムからのgnuplotを実行するか、C++プログラムの実行後にそれを実行するために?最初の場合は

system("gnuplot gnuplot_command_file"); 

returnの直前に追加してください。もちろん、まず文字列をsystemステートメントのパラメーターとして構築する必要があります。

そうでない場合は、ちょうど私のC++のプログラムからgnuplotを実行するために、あなたのコマンドプロンプトで

gnuplot your_command_file 
+0

を実行します。文字列を作成してシステムを追加するだけですか( "gnuplot command_file");私はGnuプロットヘッダファイルも追加しなければならないと思いますか? – Rick

+0

gnuplot istコマンドラインやプログラムから実行できるプログラムです。そうです。 –

+0

私はちょうどあなたが言及したように、私はコードを実行するが、まだグラフは表示されませんが、プログラムは正常に実行されます。 P.S. :Visual Studio2015を使用してプログラムを実行しています – Rick

関連する問題