|
|
Line 567: |
Line 567: |
| == 14.11.2011 Processing sketches == | | == 14.11.2011 Processing sketches == |
|
| |
|
| ===Class to import data files=== | | === READING DATA FROM A TEXTFILE === |
| This class imports tab seperated data files
| |
| into an array and allows easy access to
| |
| rows and columns.
| |
|
| |
|
| <source lang="java">
| | Imports a data table from a textfile |
| class Table {
| | with tab seperated values (tsv). <br /> These |
| int rowCount;
| | formats are very common when exporting |
| String[][] data;
| | spreadsheets from Excel or similar software.<br /> |
|
| | Make sure that the file "measurements.tsv" |
|
| | exists in the data folder of this sketch. |
| 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);
| |
| }
| |
|
| |
|
| | The Table class is written by Ben Fry: |
| | http://benfry.com/writing/archives/3<br /> |
| | It handles the import and access to the data. |
| | |
| | Download: |
| | [[Media:PhysicalComp11_reading_tsv.zip]] |
|
| |
|
| 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);
| |
| }
| |
| }
| |
| </source>
| |
| <br /><br /> | | <br /><br /> |
|
| |
|