We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Counting rows from .tsv files
Page Index Toggle Pages: 1
Counting rows from .tsv files (Read 734 times)
Counting rows from .tsv files
Apr 10th, 2010, 11:27pm
 
Hi everyone,

I have been working on my own data lately that I have acquired from motion tracking of videos. I have used Ben Fry's book on "Visualizing Data", as I am new to processing. I would like the rowCount to couple two sequential rows, so that I can have x1 and y1 and x2 and y2  so that separate lines can be drawn to illustrate the amount of motion that takes place from frame to frame. In each row of my table there is a x and y coordinate in different columns each. I keep getting the error:

"Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 8460
     at granny$Table.getString(granny.java:102)
     at granny$Table.getFloat(granny.java:127)
     at granny.draw(granny.java:37)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Thread.java:613)"

Your help would be greatly appreciated! Here is my code:

-------
Table locationTable;
int rowCount;


void setup() {
 size (720, 480);
 locationTable= new Table ("granny.tsv");
 rowCount = locationTable.getRowCount();
}

void draw() {
 background(255);
 smooth();
 noFill();
 stroke(0);
 strokeWeight(1);
 for (int row = 0; row < rowCount; row ++) {
 float x1= locationTable.getFloat (row, 1);
 float y1 = locationTable.getFloat (row, 2);
 float x2 = locationTable.getFloat (row+1, 1);
 float y2 = locationTable.getFloat (row+1, 2);
 line (x1,y1,x2,y2);
}
}

class Table {
 String[][] data;
 int rowCount;


 Table() {
   data = new String[10][10];
 }


 Table(String filename) {
   String[] rows = loadStrings(filename);
   data = new String[rows.length][];

   for (int i = 1; i < rows.length; i++) {
     if (trim(rows[i]).length() == 0) {
       continue; // skip empty rows
     }
     if (rows[i].startsWith("#")) {
       continue;  // skip comment lines
     }

     // split the row on the tabs
     String[] pieces = split(rows[i], TAB);
     // copy to the table array
     data[rowCount] = pieces;
     rowCount++;

     // this could be done in one fell swoop via:
     //data[rowCount++] = split(rows[i], TAB);
   }
   // resize the 'data' array as necessary
   data = (String[][]) subset(data, 0, rowCount);
 }


 int getRowCount() {
   return rowCount;
 }


 // find a row by its name, returns -1 if no row found
 int getRowIndex(String name) {
   for (int i = 0; i < rowCount; i++) {
     if (data[i][0].equals(name)) {
       return i;
     }
   }
   println("No row named '" + name + "' was found");
   return -1;
 }


 String getRowName(int row) {
   return getString(row, 0);
 }


 String getString(int rowIndex, int column) {
   return data[rowIndex][column];
 }


 String getString(String rowName, int column) {
   return getString(getRowIndex(rowName), column);
 }


 int getInt(String rowName, int column) {
   return parseInt(getString(rowName, column));
 }


 int getInt(int rowIndex, int column) {
   return parseInt(getString(rowIndex, column));
 }


 float getFloat(String rowName, int column) {
   return parseFloat(getString(rowName, column));
 }


 float getFloat(int rowIndex, int column) {
   return parseFloat(getString(rowIndex, column));
 }


 void setRowName(int row, String what) {
   data[row][0] = what;
 }


 void setString(int rowIndex, int column, String what) {
   data[rowIndex][column] = what;
 }


 void setString(String rowName, int column, String what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = what;
 }


 void setInt(int rowIndex, int column, int what) {
   data[rowIndex][column] = str(what);
 }


 void setInt(String rowName, int column, int what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }


 void setFloat(int rowIndex, int column, float what) {
   data[rowIndex][column] = str(what);
 }


 void setFloat(String rowName, int column, float what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }


 // Write this table as a TSV file
 void write(PrintWriter writer) {
   for (int i = 0; i < rowCount; i++) {
     for (int j = 0; j < data[i].length; j++) {
       if (j != 0) {
         writer.print(TAB);
       }
       if (data[i][j] != null) {
         writer.print(data[i][j]);
       }
     }
     writer.println();
   }
   writer.flush();
 }
}
---------
Re: Counting rows from .tsv files
Reply #1 - Apr 11th, 2010, 1:31am
 
Quote:
Code:
for (int row = 0; row < rowCount; row ++) {
float x1= locationTable.getFloat (row, 1);
float y1 = locationTable.getFloat (row, 2);
float x2 = locationTable.getFloat (row+1, 1);
float y2 = locationTable.getFloat (row+1, 2);
line (x1,y1,x2,y2);
}

It won't work, you go beyond rowCount this way. Beside, if I understood correctly, your data is interleaved, so you must count by pairs of rows. Something like:
Code:
for (int row = 0; row < rowCount / 2; row += 2) {
// Same content
}
Re: Counting rows from .tsv files
Reply #2 - Apr 11th, 2010, 1:32pm
 
Thank you so very much PhiLho, it seems to me that it works!
Smiley
Page Index Toggle Pages: 1