QCustomPlot Discussion and Comments

how to read x and y value from text file and plot graph from itReturn to overview

i have file.txt file that contain
X y
1 0.2
2 0.4
3 0.6
4 0.8
5 1
6 1.2
7 1.4
8 1.6
9 1.8
10 4

i am trying to plot graph from that x and y value in qt.
how can i do it?
i am trying this.....

 //open file
    QFile file("C:\\Users\\xyz\\Desktop\\file.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    //get data from file and stored as text stream
    QTextStream in(&file);

    while (!in.atEnd()) {
         QString line = in.readLine();// read first line and so on
         //qDebug() << line;
          QStringList fields = line.split(',');// split the string
          qDebug()<<fields.at(0).toInt();
          qDebug()<<fields.at(1).toInt();

          x =  fields.at(0).toInt(); // take first value and stored into x
          y =  fields.at(1).toInt(); // take second value and stored into y

          qDebug() << x;
          qDebug() << y;
    }
    // create graph and assign data to it:
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setData(x, y);

    // give the axes some labels:
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");

    // set axes ranges, so we see all data:
    ui->customPlot->xAxis->setRange(0, 5);
    ui->customPlot->yAxis->setRange(0, 10);

    // replot graph
    ui->customPlot->replot();
}

in these x and y are int variable.
problem this that setData take QVector<double> as a parameter so how can i read these x and y value from txt file and stored as Qvector<double>

got error like "no matching function for call to 'QCPGraph::setData(int ,int);"
anyone know about this how to do. ?

Your data is floating point, so you shouldn't be reading it in as int in the first place. Rather use double, QVector<double> and fields.at(...).toDouble() in your code.

thank you Dermanu.
i got it after reading documentation of Qt in that QVector and all that things.

once again thank you for your reply..

QVector<double> x(10), y(10);
QFile file("D:\\qt_workspace\\customplot\\a.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
int i=0;
//get data from file and stored as text stream
QTextStream in(&file);


while (!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
QStringList fields = line.split(',');
qDebug()<<fields.at(0).toDouble();
qDebug()<<fields.at(1).toDouble();

x = fields.at(0).toDouble();
y = fields.at(1).toDouble();
i=i+1;
qDebug() << x;
qDebug() << y;
}
// create graph and assign data to it:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);

// give the axes some labels:
ui->customPlot->xAxis->setLabel("x");
ui->customPlot->yAxis->setLabel("y");

// set axes ranges, so we see all data:
ui->customPlot->xAxis->setRange(0, 5);
ui->customPlot->yAxis->setRange(0, 10);

// replot graph
ui->customPlot->replot();


Still No output is coming...Please tell me if any reason behind this..

Kavita.
Did you check the while loop used here. You have to add ui->customPlot->graph(0)->setData(x, y); inside the loop.

Regards
Ansif

Hi,

Sorry for my previous answer.

See below. x and y are vectors which stores large number of values. This will be added to ui->customPlot->graph(0)->setData(x, y).

QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x = i/50.0 - 1; // x goes from -1 to 1
y = x*x; // let's plot a quadratic function
}

Regards
Ansif