|
From: <am...@gm...> - 2014-02-15 06:20:41
|
Dear All,
Please have a look at the C++ code to generate a binary data which can be plotted by the
gnuplot. I am having trouble in introducing a blank line in the binary file so that gnuplot
can plot it as pm3d map.
Thank you,
//////////////////////////////////////////////////////////////////////////////////////////////
/*
A 2D binary data generated by the code can be plotted by
gnuplot> p 'dataBinary.dat' binary format='%f%f%f' u 1:3 w l
which is equivalent to plot the ASCII data as
gnuplot> p 'dataAscii.dat' u 1:3 w l
The pm3d map plot can be plotted for ASCII data as follows,
provided one should have a blank line after each set of data.
Blank line should be introduced after the completion of inner
'for loop' in the following code.
gnuplot> set pm3d map;
gnuplot> sp 'dataAscii.dat' u 1:2:3 w pm3d;
How one can plot the pm3d data in binary file format. The
only thing missing is the introduction of blank line in the
binary data which should be understood by the gnuplot.
gnuplot> set pm3d map;
gnuplot> sp 'dataBinary.dat' binary format='%f%f%f' u 1:2:3 w pm3d;
The above command does not plot a pm3d from binary data as there
is a blank line missing in it.
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <cstdio>
#include <iomanip>
using namespace std;
struct Data
{
float x,y,z;
};
int main()
{
ofstream outf("dataBinary.dat",ios::binary);
//ofstream outf("dataAscii.dat",ios::out);
Data myData[100];
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
myData[j].x = i;
myData[j].y = j;
myData[j].z = sqrt(i+j);
// writing ASCII data
//outf << endl << myData[j].x << setw(15) << myData[j].y << setw(15) << myData[j].z;
}
//outf << endl ; // for ascii data to be plotted as pm3d by gnuplot
// writing binary data
outf.write((char *)(&myData),sizeof(myData));
}
outf.close();
cout << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
|