PDA

View Full Version : Qwtplot: reading data from text file



GG2013
31st May 2013, 07:03
Hallo all,
I am trying to use Qwtplot to plot some data which are in a .txt file. To keep things simple, my data file is as below- the 1st column is x-data and the 2nd is y-data.

data.txt

1.5 1.9
2.2 2.8
3.1 10.2
4.3 5.5

When I use the following code (i.e x & y values are put straight in QPointF()), I get the plot nice and well.



void Traffic::plotData()
{
QwtPlot *plot = new QwtPlot(QString("Curves"));
this->setCentralWidget(plot);

plot->setTitle( "Plot Demo" );
plot->setCanvasBackground( Qt::white );
plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0);
plot->insertLegend( new QwtLegend() );

QwtPlotGrid *grid = new QwtPlotGrid();
grid->attach(plot );

QwtPlotCurve *curve = new QwtPlotCurve();
curve->setTitle( "Pixel Count" );
curve->setPen( Qt::blue, 4 ),
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );

QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
curve->setSymbol( symbol );

QPolygonF points;
points << QPointF(1.5, 1.9 ) << QPointF( 2.2, 2.8 )
<< QPointF( 3.1, 10.2 ) << QPointF( 4.3, 5.5 );

curve->setSamples(points);
curve->attach(plot );
plot->replot();

plot->resize( 600, 400 );
plot->show();
}


However, if I use the following code to get x & y values then the values are not read correctly - see the debugger output.


vector<double> X, Y;
double x, y;

ifstream fp("data.txt");

while (!fp.eof())
{
fp >> x>> y;
X.push_back(x);
Y.push_back(y);
qDebug()<<"Hi";
qDebug()<<x <<","<<y;

if (!fp)
break;
}

"Debugging starts
&"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
Hi
1.30547e-312 , 2.76677e-322
Debugging has finished"

I also tried to use QFile, QTextStream etc. but then the values read are empty.

Any help/suggestion would be greatly appreciated (a simple example code would be great!).

Added after 58 minutes:

Sorry guys..I found the problem. Previously data.txt was located in pwd but when I placed in the build directory the data was read correctly.