How to make a csv file with test data

edited December 2015 in Questions about Code

Okay so i am collecting some data with and arduino and sending them to processing. Here they are stored in an int array. I want to put this int array down in one column with a header (String) in a CSV file. But i want to be able to keep adding columns with data, as the data comes in from the arduino. But i am confused by the row.setInt(nameOfData, array[i]) function. Why can't i, like the .getInt(row, column), specify which column and row i wan't to do manipulation with?

My code looks like this:

int hammerArDatRaw[] = {
  2000, 2100, 2200, 2300, 3000, 4000, 3000
};

Table table;
TableRow row;

String nameOfExperiment = "Experiment";
int numberOfStrokeSaved = 0;



void setup() {
  table = new Table();
  WriteDataToCSVFile(hammerArDatRaw, "kkk");
  WriteDataToCSVFile(hammerArDatRaw, "skr");
  makeCSVFile(nameOfExperiment);
}

void makeCSVFile(String name) {
  saveTable(table, "data/"+nameOfExperiment+".csv");
}

void WriteDataToCSVFile(int array[], String nameOfData) {
  table.addColumn(nameOfData, Table.INT);
  row = table.addRow();


  for (int i = 0; i< array.length; i++) {
    row.setInt(nameOfData, array[i]);
    row = table.addRow();
  }
}

As one can if they open the CSV file it looks like this:

kkk skr 2000 0 2100 0 2200 0 2300 0 3000 0 4000 0 3000 0 0 0 0 2000 0 2100 0 2200 0 2300 0 3000 0 4000 0 3000 0 0

(Okay the quote system turned the columns sideways, so flip it 90 deg and you see my problem).

I don't want the zeroes but want the data to begin from the top everytime i call WriteDataToCSVFile(). What am i doing wrong?

Answers

  • edited November 2014

    Class Table was meant for fixed # of columns and variable # of rows. Seems like you want the opposite of it! :-?
    If you wanna bend that "rule", when you addColumn(), you gotta insert each array element in a different row in that same added column. You don't addRow() but re-use the existing fixed 1s!
    You gotta change your current algorithm I'm afraid! ^#(^

  • edited November 2014

    ---- Dont read this, i managed to fix it ------

    Dear GoToLoop. Thanks for your answer.

    I think i got what you were saying, so i changed my code so i start out making the rows, and then put in my columns. Its no dealbreaker that i have to define the number of rows first, since this will be the same for all data collections. But then it seems like the "virtual cursor" is set at the bottom row. You can see that it prints a lot of zeroes and then at the bottom overwrites the cell until last number in the array is standing. How do i get the "cursor" to be at the row i wan't to manipulate?

    int hammerArDatRaw[] = {
      2000, 2100, 2200, 2300, 3000, 4000, 3000
    };
    
    Table table;
    TableRow row;
    
    String nameOfExperiment = "Experiment";
    int numberOfStrokeSaved = 0;
    
    void setup() {
      table = new Table();
      makeCSVFileRows(nameOfExperiment, hammerArDatRaw);
      WriteDataToCSVFile(hammerArDatRaw, "kkk");
      WriteDataToCSVFile(hammerArDatRaw, "skr");
      saveTable(table, "data/"+nameOfExperiment+".csv");
    }
    
    void makeCSVFileRows(String name, int array[]) {      
      for (int i = 0; i< array.length; i++) {
        row = table.addRow();
      }    
    }
    
    void WriteDataToCSVFile(int array[], String nameOfData) {
      table.addColumn(nameOfData, Table.INT);
      for (int i = 0; i< array.length; i++) {
        row.setInt(nameOfData, array[i]);
      }
    }
    

    And the subsequent data:

    kkk skr

    0 0

    0 0

    0 0

    0 0

    0 0

    0 0

    3000 3000

  • edited November 2014

    ---- Dont read this, just me making mistakes ------

    Okay on this page: https://www.processing.org/reference/Table_setInt_.html

    Its says that the setInt syntax are as follows:

    .setInt(row, column, value)

    .setInt(row, columnName, value)

    But everytime i add a .setInt(int, int, int) or a .setInt(int, string, int) it says that it is only applicable for two arguments. Am i doing it wrong or is this a mistake?

  • Okay i am officially an idiot. I typed row in front of the .setInt(row, column, value), instead of table. Now it works. Thank you for your help an sorry for being such a derp :)

  • Hi i copy your code and chancge row to table and i have an error can you write the right code please?

  • int hammerArDatRaw[] = {
      2000, 2100, 2200, 2300, 3000, 4000, 3000
    };
    
    Table table;
    TableRow row;
    
    String nameOfExperiment = "Experiment";
    int numberOfStrokeSaved = 0;
    
    void setup() {
      table = new Table();
    
      // make all empty rows first
      makeCSVFileRows(nameOfExperiment, hammerArDatRaw);
    
      // add 2 columns
      WriteDataToCSVFile(hammerArDatRaw, "kkk");
      WriteDataToCSVFile(hammerArDatRaw, "skr");
    
      // save 
      saveTable(table, "data/"+nameOfExperiment+".csv");
    
      println ("done.");
    }
    
    void makeCSVFileRows(String name, int array[]) {      
      for (int i = 0; i< array.length; i++) {
        row = table.addRow();
      }
    }
    
    void WriteDataToCSVFile(int array[], String nameOfData) {
      table.addColumn(nameOfData, Table.INT);
      for (int i = 0; i< array.length; i++) {
        table.setInt(i, nameOfData, array[i]);
      }//for
    }//func
    //
    
  • i need you help i dont now why i cant print a row, when i compile the program it says the function setfloat(int,int,floar) does not exist

    import processing.serial.*; Serial mySerial; Table table; TableRow row; float valor=0; PrintWriter output; String value; float inByte=0; void setup() { table=new Table(); mySerial = new Serial( this, Serial.list()[0], 9600 ); output = createWriter( "data1.csv" ); table.addColumn("Num"); table.addColumn("Press"); table.addRow(); saveTable(table,"data/Prueba.csv"); } void draw() {

      value=mySerial.readStringUntil('\n');
      println(Serial.list());
         if ( value != null ) 
         {
              value=trim(value);
              inByte=float(value);
              println(inByte);
              output.println(inByte);
              row.setfloat(table.lastRowIndex(),Press,inByte);
    
         }
    

    } void keyPressed() { output.flush(); output.close(); exit();
    }

  • import processing.serial.*; Serial mySerial; Table table; TableRow row; float valor=0; PrintWriter output; String value; float inByte=0; void setup() { table=new Table(); mySerial = new Serial( this, Serial.list()[0], 9600 ); output = createWriter( "data1.csv" ); table.addColumn("Num"); table.addColumn("Press"); table.addRow(); saveTable(table,"data/Prueba.csv"); } void draw() {

      value=mySerial.readStringUntil('\n');
    
         if ( value != null ) 
         {
              inByte=float(value);
              output.println(inByte);
              valor=inByte*5;
              println(valor);
              row.setfloat(table.lastRowIndex(),Press,inByte);
         }
    

    } void keyPressed() { output.flush(); output.close(); exit();
    }

  • edited December 2015 Answer ✓

    when you want this

    .setFloat(row, columnName, value)

    like described here:

    https://www.processing.org/reference/Table_setFloat_.html

    instead of

    row.setfloat(table.lastRowIndex(),Press,inByte);

    you maybe want

    row.setfloat(table.lastRowIndex(),"Press",inByte);

    with "" since "Press" is a String and the name of the column

Sign In or Register to comment.