Reading data from a .csv file

Hey ! How do we go about extracting data from a particular row and column of a .csv file ? Secondly, is it possible to extract a whole row of data from the file and put it in a String ?

How do we go about extracting data from a particular row and column of a .csv file ?

Set a counter to -1. Read data from the file. Store it in an array, unless the character is a carriage return or line feed. If it is, increment the counter.

Is the counter value the same as the one you want to process? If not, keep reading.

If so, use the data. The column is an excel thing, not a text file thing, in general.

The column is an excel thing, not a text file thing, in general.

I'm quiet aware of that. The thing is, I decided to open the saved data in Excel because of the Scatter Plot. As I may have taken off on a wrong start, do permit me to make amends. How do we go about extracting the data from the file ? What kind of function do we utilize ?

adeelshams:
I’m quiet aware of that. The thing is, I decided to open the saved data in Excel because of the Scatter Plot.
As I may have taken off on a wrong start, do permit me to make amends. How do we go about extracting the data from the file ? What kind of function do we utilize ?

The exact method depends on how you want to select the data.

If you treat the csv data as a ‘x’ by ‘y’ array of cstring:

/* returns (positive) length of element
    -1 open failed
    -2 eof (y value too big)
    -3 eol (x value too big)
    -4 maxLen too small

maxLen does not count the terminal null, so if maxLen is 10, result needs to be char[11]
result is a buffer, maxLen +1 in length were the element is returned

*/
int16_t getElement(char* name, uint16_t x, uint16_t y, char & result, uint8_t maxLen){
File inCSV = SD.open(name,FILE_OPEN);
if(inCSV){ // successful open
  uint16_t currLine=1, currCol=1;
  bool done=false,error=false;
  while((currLine<y)&&(!error)){ // scan thru file counting lf
    error=!inCSV.available();
    if(!error){ // file not at end
      char ch = inCSV.read();
      if(ch=='\n') currLine++;
      }
    }
  if(error){
   inCSV.close();
   return -2; // end of file encountered before specified row
   }
  // else successfully found row, now looking for column.
  while((currCol<x)&&(!error)){// scan thru current, counting commas
    error=!inCSV.available();
    if(!error){ // not at end
      char ch = inCSV.read();
      if(ch==',') currCol++;
      else if(ch=='\n') error=true; // eol encountered
      }
    }
  if(error){ // end of file, end of line encountered
    inCSV.close();
    return -3;
    }
 // else successfully found cell, now move to result while it fits
 uint8_t pos=0;
 while((pos<maxLen)&&(!error)&&(!done)){
  error=!inCSV.available();
  if(!error){
    char ch = inCSV.read();
    if (ch==',') done = true; // successful!
    else if(ch=='/n') done = true; // also successful!
    else {
      result[pos] = ch;
      pos++;
      }
    }
  else { // not actually an error, just end of file
    error = false;
    done = true;
    }
  }
  if(done) {
    result[pos+1]='\0'; //append terminal null
    inCSV.close();
    return pos;
    }
  else {// too big to fit
    result[pos]='\0';
    inCSV.close();
    return -4;
    }
  }
else return -1;
}

This code has not been complied, probable syntax errors.
But, you should be able to understand it. Also, it DOES not handle “quoted” text.

Chuck.