Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how i can identify and read the columns of a csv file ?
Forum Updated to NodeBB v4.3 + New Features

how i can identify and read the columns of a csv file ?

Scheduled Pinned Locked Moved Solved General and Desktop
49 Posts 8 Posters 7.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J julie546
    1 Jul 2021, 15:00

    @KroMignon yes it's really difficult to work in a field that you don't study
    thank you for your answer I tried like that but I still have an empty curve

    if (!filelidar.open(QFile::ReadOnly | QFile::Text) ) {
    qDebug() << "File not exists";
    } else {

                       QLineSeries * series = new QLineSeries();
                               while (!filelidar.atEnd()) {
                                   QString line = filelidar.readLine();
                                   const auto parts = line.split (',');
                                   if(parts.size() >= 2)
                                   {
                                       bool ok;
                                       qreal x = parts.at(0).toFloat(&ok);
                                       if(!ok)
                                           continue;
                                       qreal y = parts.at(1).toFloat(&ok);
                                       if(!ok)
                                           continue;
                                      series->append(x, y);
                                   }
                               }
    
    
    
                           QChart *chart = new QChart(); 
                           chart->legend()->hide();
                           chart->addSeries(series);
                           chart->createDefaultAxes();
                           chart->setTitle("Lidar tg15"); 
    
                           QChartView *chartView = new QChartView(chart);
                           chartView->setRenderHint(QPainter::Antialiasing);
    
                           QMainWindow window; 
                           window.setCentralWidget(chartView); 
                           window.resize(1000, 1000); 
                           window.show(); /
    

    }
    }

    P Offline
    P Offline
    Pl45m4
    wrote on 1 Jul 2021, 15:07 last edited by Pl45m4 7 Jan 2021, 15:10
    #20

    @julie546 said in how i can identify and read the columns of a csv file ?:

    qreal x = parts.at(0).toFloat(&ok);
    if(!ok)
    continue;
    qreal y = parts.at(1).toFloat(&ok);

    Print the values for x and y and check if these are the values from your file.
    The code from @KroMignon doesn't look wrong.


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    J 1 Reply Last reply 1 Jul 2021, 15:27
    0
    • P Pl45m4
      1 Jul 2021, 15:07

      @julie546 said in how i can identify and read the columns of a csv file ?:

      qreal x = parts.at(0).toFloat(&ok);
      if(!ok)
      continue;
      qreal y = parts.at(1).toFloat(&ok);

      Print the values for x and y and check if these are the values from your file.
      The code from @KroMignon doesn't look wrong.

      J Offline
      J Offline
      julie546
      wrote on 1 Jul 2021, 15:27 last edited by julie546 7 Jan 2021, 15:27
      #21

      @Pl45m4 @KroMignon yes I have checked I have the right values ​​now I have a graph but it is not the same that I do with the csv file, I cannot find the same graph, what do you think please ?

      K 1 Reply Last reply 1 Jul 2021, 15:32
      0
      • J julie546
        1 Jul 2021, 15:27

        @Pl45m4 @KroMignon yes I have checked I have the right values ​​now I have a graph but it is not the same that I do with the csv file, I cannot find the same graph, what do you think please ?

        K Offline
        K Offline
        KroMignon
        wrote on 1 Jul 2021, 15:32 last edited by
        #22

        @julie546 said in how i can identify and read the columns of a csv file ?:

        but it is not the same that I do with the csv file,

        What do you mean with not the same?

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        J 1 Reply Last reply 1 Jul 2021, 15:38
        1
        • K KroMignon
          1 Jul 2021, 15:32

          @julie546 said in how i can identify and read the columns of a csv file ?:

          but it is not the same that I do with the csv file,

          What do you mean with not the same?

          J Offline
          J Offline
          julie546
          wrote on 1 Jul 2021, 15:38 last edited by
          #23

          @KroMignon this is what I found with qt1.PNG

          and this is what I have to find
          2.PNG

          despite I made qDebug () << x;
          qDebug () << y;
          and I found the same values with csv file

          J 1 Reply Last reply 1 Jul 2021, 15:49
          0
          • J julie546
            1 Jul 2021, 15:38

            @KroMignon this is what I found with qt1.PNG

            and this is what I have to find
            2.PNG

            despite I made qDebug () << x;
            qDebug () << y;
            and I found the same values with csv file

            J Offline
            J Offline
            julie546
            wrote on 1 Jul 2021, 15:49 last edited by julie546 7 Jan 2021, 15:49
            #24
            This post is deleted!
            M 1 Reply Last reply 1 Jul 2021, 15:50
            0
            • J julie546
              1 Jul 2021, 15:49

              This post is deleted!

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Jul 2021, 15:50 last edited by
              #25

              @julie546
              Hi
              If you look closely they are the same.
              Its just you are using QLineSeries which draws lines between the points so
              that why it looks differernt.

              Maybe the scatter type can be used
              https://doc.qt.io/qt-5/qtcharts-scatterchart-example.html

              instead of the LineSeries.

              J 1 Reply Last reply 1 Jul 2021, 16:00
              2
              • M mrjj
                1 Jul 2021, 15:50

                @julie546
                Hi
                If you look closely they are the same.
                Its just you are using QLineSeries which draws lines between the points so
                that why it looks differernt.

                Maybe the scatter type can be used
                https://doc.qt.io/qt-5/qtcharts-scatterchart-example.html

                instead of the LineSeries.

                J Offline
                J Offline
                julie546
                wrote on 1 Jul 2021, 16:00 last edited by
                #26

                @mrjj
                thank you for your reply I have changed it but there are some missing points3.PNG
                QScatterSeries * series = new QScatterSeries;
                series->setMarkerShape(QScatterSeries::MarkerShapeRectangle);

                M S 2 Replies Last reply 1 Jul 2021, 16:03
                0
                • J julie546
                  1 Jul 2021, 16:00

                  @mrjj
                  thank you for your reply I have changed it but there are some missing points3.PNG
                  QScatterSeries * series = new QScatterSeries;
                  series->setMarkerShape(QScatterSeries::MarkerShapeRectangle);

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 1 Jul 2021, 16:03 last edited by
                  #27

                  @julie546
                  Well it does look more like it
                  so the question if those points go missing during reading or where they go.

                  What does the draw correct looking diagram ?
                  I mean what app ?

                  J 1 Reply Last reply 1 Jul 2021, 16:10
                  0
                  • M mrjj
                    1 Jul 2021, 16:03

                    @julie546
                    Well it does look more like it
                    so the question if those points go missing during reading or where they go.

                    What does the draw correct looking diagram ?
                    I mean what app ?

                    J Offline
                    J Offline
                    julie546
                    wrote on 1 Jul 2021, 16:10 last edited by
                    #28

                    @mrjj it is a 'lidar tg15' sensor
                    i did series->append(x, y);
                    qDebug() << y;
                    qDebug() << x;
                    and I found all the points

                    M 1 Reply Last reply 1 Jul 2021, 16:13
                    0
                    • J julie546
                      1 Jul 2021, 16:10

                      @mrjj it is a 'lidar tg15' sensor
                      i did series->append(x, y);
                      qDebug() << y;
                      qDebug() << x;
                      and I found all the points

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 1 Jul 2021, 16:13 last edited by
                      #29

                      @julie546
                      Ok but the right diagram you showed the picture of.
                      Did you hand draw this or did an app do it?

                      since no points is missing im simply wondering if that data would allow the correct diagram at all.

                      J 1 Reply Last reply 1 Jul 2021, 16:21
                      0
                      • M mrjj
                        1 Jul 2021, 16:13

                        @julie546
                        Ok but the right diagram you showed the picture of.
                        Did you hand draw this or did an app do it?

                        since no points is missing im simply wondering if that data would allow the correct diagram at all.

                        J Offline
                        J Offline
                        julie546
                        wrote on 1 Jul 2021, 16:21 last edited by
                        #30

                        @mrjj 2.PNG
                        it's from excel

                        M 1 Reply Last reply 1 Jul 2021, 16:27
                        0
                        • J julie546
                          1 Jul 2021, 16:21

                          @mrjj 2.PNG
                          it's from excel

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 1 Jul 2021, 16:27 last edited by
                          #31

                          @julie546
                          ok. and you used the same CSV file there ?

                          and you compared the values we insert, value by value to make sure it reads as it should ?

                          I mean if it could not convert for some reason it would silently continue.

                           if(!ok)
                             continue;
                          

                          some of the points to the far right seems more out there than on correct diagram.
                          should be around 6.0 but is 6.8

                          J 1 Reply Last reply 1 Jul 2021, 16:34
                          2
                          • M mrjj
                            1 Jul 2021, 16:27

                            @julie546
                            ok. and you used the same CSV file there ?

                            and you compared the values we insert, value by value to make sure it reads as it should ?

                            I mean if it could not convert for some reason it would silently continue.

                             if(!ok)
                               continue;
                            

                            some of the points to the far right seems more out there than on correct diagram.
                            should be around 6.0 but is 6.8

                            J Offline
                            J Offline
                            julie546
                            wrote on 1 Jul 2021, 16:34 last edited by
                            #32

                            @mrjj yes it's from the same csv file
                            i have on the csv file 1500 value

                            J 1 Reply Last reply 1 Jul 2021, 16:37
                            0
                            • J julie546
                              1 Jul 2021, 16:34

                              @mrjj yes it's from the same csv file
                              i have on the csv file 1500 value

                              J Offline
                              J Offline
                              JonB
                              wrote on 1 Jul 2021, 16:37 last edited by
                              #33

                              @julie546
                              As @mrjj said, just to humour us, in the two places you have

                                      if(!ok)
                                          continue;
                              

                              please put in above the if

                              Q_ASSERT(ok);
                              

                              (You'll need to have included #include <QtGlobal>, but probably something else you are including will have done this.)

                              K 1 Reply Last reply 1 Jul 2021, 16:52
                              1
                              • J JonB
                                1 Jul 2021, 16:37

                                @julie546
                                As @mrjj said, just to humour us, in the two places you have

                                        if(!ok)
                                            continue;
                                

                                please put in above the if

                                Q_ASSERT(ok);
                                

                                (You'll need to have included #include <QtGlobal>, but probably something else you are including will have done this.)

                                K Offline
                                K Offline
                                KroMignon
                                wrote on 1 Jul 2021, 16:52 last edited by
                                #34

                                @JonB said in how i can identify and read the columns of a csv file ?:

                                As @mrjj said, just to humour us, in the two places you have
                                if(!ok)
                                continue;

                                please put in above the if
                                Q_ASSERT(ok);

                                The reason why there is continue, is because the CSV file contains the column headers, which are strings and cannot be converted to float.
                                With Q_ASSERT() the program will be interrupted.

                                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                M J 2 Replies Last reply 1 Jul 2021, 17:01
                                1
                                • K KroMignon
                                  1 Jul 2021, 16:52

                                  @JonB said in how i can identify and read the columns of a csv file ?:

                                  As @mrjj said, just to humour us, in the two places you have
                                  if(!ok)
                                  continue;

                                  please put in above the if
                                  Q_ASSERT(ok);

                                  The reason why there is continue, is because the CSV file contains the column headers, which are strings and cannot be converted to float.
                                  With Q_ASSERT() the program will be interrupted.

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 1 Jul 2021, 17:01 last edited by
                                  #35

                                  @KroMignon
                                  ah so it should skip first col. always. good catch.
                                  Im just wondering if all points really get converted as
                                  else i dont see how the graphs can be that differnt.

                                  1 Reply Last reply
                                  0
                                  • K KroMignon
                                    1 Jul 2021, 16:52

                                    @JonB said in how i can identify and read the columns of a csv file ?:

                                    As @mrjj said, just to humour us, in the two places you have
                                    if(!ok)
                                    continue;

                                    please put in above the if
                                    Q_ASSERT(ok);

                                    The reason why there is continue, is because the CSV file contains the column headers, which are strings and cannot be converted to float.
                                    With Q_ASSERT() the program will be interrupted.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 1 Jul 2021, 18:06 last edited by
                                    #36

                                    @KroMignon said in how i can identify and read the columns of a csv file ?:

                                    The reason why there is continue, is because the CSV file contains the column header

                                    Surely that's for the first line only?

                                    J 1 Reply Last reply 2 Jul 2021, 07:03
                                    0
                                    • J JonB
                                      1 Jul 2021, 18:06

                                      @KroMignon said in how i can identify and read the columns of a csv file ?:

                                      The reason why there is continue, is because the CSV file contains the column header

                                      Surely that's for the first line only?

                                      J Offline
                                      J Offline
                                      julie546
                                      wrote on 2 Jul 2021, 07:03 last edited by
                                      #37

                                      @JonB @mrjj @KroMignon
                                      I remove the headers and I replace
                                      if (! ok)
                                      continue;
                                      by Q_ASSERT (ok);
                                      but it's the same result

                                      K 1 Reply Last reply 2 Jul 2021, 07:10
                                      0
                                      • J julie546
                                        2 Jul 2021, 07:03

                                        @JonB @mrjj @KroMignon
                                        I remove the headers and I replace
                                        if (! ok)
                                        continue;
                                        by Q_ASSERT (ok);
                                        but it's the same result

                                        K Offline
                                        K Offline
                                        KroMignon
                                        wrote on 2 Jul 2021, 07:10 last edited by
                                        #38

                                        @julie546 said in how i can identify and read the columns of a csv file ?:

                                        I remove the headers and I replace
                                        if (! ok)
                                        continue;
                                        by Q_ASSERT (ok);
                                        but it's the same result

                                        I suppose it is a simple precision issue, you should replace toFloat() with toDouble():

                                        while (!filelidar.atEnd()) {
                                            QString line = filelidar.readLine();
                                            const auto parts = line.split (',');
                                            if(parts.size() >= 2)
                                            {
                                                bool ok;
                                                qreal x = parts.at(0).toDouble(&ok);
                                                if(!ok)
                                                {
                                                    qDebug() << "Could not parse X value" << parts.at(0);
                                                    continue;
                                                }
                                                qreal y = parts.at(1).toDouble(&ok);
                                                if(!ok)
                                                {
                                                    qDebug() << "Could not parse Y value" << parts.at(1);
                                                    continue;
                                                }
                                                series->append(x, y);
                                            }
                                        }
                                        

                                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                        J 1 Reply Last reply 2 Jul 2021, 07:28
                                        0
                                        • K KroMignon
                                          2 Jul 2021, 07:10

                                          @julie546 said in how i can identify and read the columns of a csv file ?:

                                          I remove the headers and I replace
                                          if (! ok)
                                          continue;
                                          by Q_ASSERT (ok);
                                          but it's the same result

                                          I suppose it is a simple precision issue, you should replace toFloat() with toDouble():

                                          while (!filelidar.atEnd()) {
                                              QString line = filelidar.readLine();
                                              const auto parts = line.split (',');
                                              if(parts.size() >= 2)
                                              {
                                                  bool ok;
                                                  qreal x = parts.at(0).toDouble(&ok);
                                                  if(!ok)
                                                  {
                                                      qDebug() << "Could not parse X value" << parts.at(0);
                                                      continue;
                                                  }
                                                  qreal y = parts.at(1).toDouble(&ok);
                                                  if(!ok)
                                                  {
                                                      qDebug() << "Could not parse Y value" << parts.at(1);
                                                      continue;
                                                  }
                                                  series->append(x, y);
                                              }
                                          }
                                          
                                          J Offline
                                          J Offline
                                          julie546
                                          wrote on 2 Jul 2021, 07:28 last edited by
                                          #39

                                          @KroMignon thank you for your answer you helped me a lot,I tried it but it's the same thing, you don't think it's about this instruction ?
                                          QScatterSeries * series = new QScatterSeries;
                                          series->setMarkerShape(QScatterSeries::MarkerShapeRectangle);

                                          K J 2 Replies Last reply 2 Jul 2021, 07:35
                                          0

                                          29/49

                                          1 Jul 2021, 16:13

                                          • Login

                                          • Login or register to search.
                                          29 out of 49
                                          • First post
                                            29/49
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved