Help with .csv / .tsv import!
in
Programming Questions
•
1 year ago
Hello all. I need a hand with a .csv / .tsv project.
I am basing much of my work off of this post & ch3 of the Ben Fry book
I have a CSV/TSV file with 6 points of data for each line in the following synax:
title,
negRadius, posRadius, neutralRadius,
x,
y
At this point columns 2, 5 & 6 are important.
5 & 6 relate to the X / Y coordinates that I would like to plot.
2 gives the radius of an ellipse to plot at the given X,Y.
Using the code (below) & data (linked) I am given an error reading: "ArrayIndexOutOfBoundsException: 6" The number changes if I alter the column value I am pulling first (2,5 or 6) so the error is probably tied to that - but I have no idea how to fix it.
Code:
- Table locationTable;
- int rowCount;
- void setup() {
- size(640, 400);
- locationTable = new Table("nyc_data.tsv");
- // The row count will be used a lot, store it locally.
- rowCount = locationTable.getRowCount();
- }
- void draw() {
- background(255);
- // Drawing attributes for the ellipses
- smooth();
- fill(192, 0, 0);
- noStroke();
- // Loop through the rows of the locations file and draw the points
- for (int row = 0; row < rowCount; row++) {
- float x = locationTable.getFloat(row, 6); // column 6
- float y = locationTable.getFloat(row, 5); // column 5
- ellipse(x, y, 9, 9);
- }
- }
With additional 'Table' code based on Fry's ch3. (shown below)
Can someone help me plot ellipses to the X,Y (column 5&6) from the .tsv? My programming ability is quite limited... but I'll do my best to work with any help I'm given.
Much appreciated!!!
pdesign
Table code:
- class Table {
- int rowCount;
- String[][] data;
- Table(String filename) {
- String[] rows = loadStrings(filename);
- data = new String[rows.length][];
- for (int i = 0; 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);
- }
- }
1