Basic Processing Sketch for accessing data from .CSV File

Below is the sketch that I used in at the end of class to demonstrate how to access data from your data file.
Please make sure to download Processing to your home computer and experiment with the sketch below and your data before coming to class on Wednesday. If you would like to do another tutorial on Processing, email me individually and we will arrange a time to meet.

As with anything – this is just one way to do it.  We will go over more methods on Wednesday.

COPY EVERYTHING BELOW THE LINE INTO YOUR PROCESSING SKETCH

*Remember to create a .CSV File and put it in a folder titled data inside the same place where you saved your sketch

*Remember you will need to rename the word “Date” in : String date = row.getString(“Date”); to match your row header. Keep in mind that the method used here calls it as a String – which means that it is a series of letters, not numbers.  If your row is a series of numbers, you would want it to write the code as Int num = row.getInt(“Num”); Num in this case is my variable for the number – but I am declaring it as an integer.

NOTE: (10/29) When copying this sketch into Processing, it does not convert the quotation marks correctly due to something strange with the webtext.  You will need to retype the quotation marks into places where you see them.

EG: IN float miles = row.getInt(“Miles”); You would replace the ” around Miles.

**************************************************************************************************

Table table;

void setup() {
size(400, 400);
background(255);
}

void draw() {
//This next step accesses the CSV file you created from the data folder
//make sure to title it “data.csv” in order to access it correctly
//And make sure that your file is within the data folder that your
//Processing Sketch is saved and is located. Reminder, you will have
//to make a folder called data.

table = loadTable(“data.csv”, “header”);

println(table.getRowCount() + ” total rows in table”);
int rowCount=table.getRowCount();
for (TableRow row : table.rows()) {
//replace “Date” with the header row you want to parse data from
String date = row.getString(“Date”);
//replace “Miles” with the header row you want to parse data from
float miles = row.getInt(“Miles”);
println(date + miles );
println(“row count:”);
//println(rowCount);
for (int i=0; i<rowCount; i++) {
noFill();
rectMode(CENTER);
stroke(miles,miles,random(0,255));
rect(200, 200, miles*10, miles*10);
}
}
}

Advertisements

One thought on “Basic Processing Sketch for accessing data from .CSV File

  1. Pingback: Project 3: Wearable Data Portraits | Fall 2015 Foundations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s