-4

I am a college student and as part of my final project for a c++ class we are assigned to read a csv file that has distance and force data and then find torque from it. The problem that I am running into is how to actually get the data out of the csv file into a workable format. Currently I have been trying to get it into a matrix, to do this though I will need to first determine the size of the csv file as it is supposed to take any size file. This is the format of the data

Case 1,,,,,x_position (m),y_position (m),z_position (m),F_x (N),F_y (N),F_z (N)16.00,5.00,8.00,394.00,-18.00,396.0022.00,26.00,14.00,-324.00,-420.00,429.0028.00,25.00,21.00,73.00,-396.00,-401.006.00,9.00,12.00,-367.00,-137.00,-143.00

Also obviously the different data pieces (distance and forces) need to be put into different vectors or matrices. This is what I have so far to try to find the number of lines in the file.

ifstream myfile("force_measurements.csv");
if(myfile.is_open()){
        string line;
        double num=0;
        getline(myfile, line);
        if(line==""){
            cout<<num<<endl;
            myfile.close();
        }
        else{
            num++;
        }
}

After this works how would you go about putting the data into a matrix? or would a different format be easier? vectors was my other though.

  • the file has to have some information on number of rows/columns in its header, otherwise you have no chance to get the dimensions right. Or is the matrix always quadratic? – 463035818_is_not_an_ai Dec 05 '17 at 20:14
  • There are always 6 columns of data. The 3 dimensions and there 3 matching dimension forces – M2Milford Dec 05 '17 at 23:07

2 Answers2

0
// you need to include the proper headers here
class vec3D:
{
     double m_x, m_y, m_z;
 public:
     void Set(size_t indx, double val)
     {
         switch(indx)
         { 
             case 0:
                 m_x = val;
                 break;
             case 1:
                 m_y = val;
                 break;
             case 2:
                 m_z = val;
                 break;
             default:
                 std::cout << "index out of range" << std::endl;
         }
     }
     double magnitude() { return std::sqrt( m_x*m_x + m_y*m_y + m_z*m_y);}
};

int main()
{
     std::ifstream my_data_file ( "your_file_here.csv" );
     std::vector<std::array<vec3D, 2>> Pos_Forz;
     if ( ! my_data_file.is_open())
     {
          std::cout << "failure to open the file" <<std::endl;
          return -1;
     }
     std::string temp_str;
     double arr[6];
     std::array<vec3D,2> temp_3Dvec;
     while(!my_data_file.eof())
     {
         for (int i = 0; i<5 ; i++)
         {
             getline(my_data_file, temp_str, ",");
             arr[i] = std::stod(temp_str);
         }
         getline(my_data_file, temp_str);
         arr[5] = std::stod(temp_str);
         for(int i = 0; i<2; i++)
         { 
             for (int j=0; j<3; j++)
             {
                   temp_3Dvec[i].set(j, arr[i*3 + j]);
             }
         }
         Pos_Forz.push_back(temp_3Dvec);
     }
    for(auto e: Pos_Forz)
    {
      std::cout << " toque = " << (e[0].magnitude() * e[1].magnitude()) <<std::endl;
    }
    return 0;
 }

Fix what need to be fixed, and inspire yourself from this code, also stop asking this kinda of questions here, you need to read the HOW TO of posting

organicoman
  • 154
  • 6
-2

You have multiple questions. Here's something I used for loading items out of a csv. This gets you started, and you can figure out whether further organization of the data is useful. I personally have classes that take a tokenized line and create an instance of themselves with them.

#include <boost/tokenizer.hpp>
typedef vector<string> csvLine;
void aFunction()
{
    string line;
    vector<csvLine> usedCollection;
    ifstream csvFile("myfile.csv", ios::in);
    if (!csvFile)
        return -1;

    while (getline(csvFile, line))
    {
        vector<string> tokenizedLine;
        boost::tokenizer<boost::escaped_list_separator<char> > tk(
            line, boost::escaped_list_separator<char>('\\', ',', '\"'));
        for (const string& str : tk)
        {
            tokenizedLine.push_back(str);
        }
        usedCollection.push_back(tokenizedLine);
    }
    csvFile.close();
}

And so usedCollection is a collection of csvLines, where each of those is a collection of strings broken up by the separator character.

zzxyz
  • 2,953
  • 1
  • 16
  • 31