This is what my data looks like in the text file:
I have decided to store each line within an ArrayList.
The first column is stored in another ArrayList by splitting the data into bits using splitTokens.
Most of the file reading code was taken from this Java site, and seems to work quite fine with the Processing programming language. I have taken bits and pieces of code from various places, and added my own flavour.
Here is a snippet of the data after import.
You can see that I have separated the two ArrayLists using dots "....."
Processing Code:
1 |
|
Processing Code Explained
void setup(): this creates 2 new ArrayLists to hold the data, and then calls the readData function. The readData() function needs the name of the file that you want to analyse, eg. C:/mySensorData.txt
br=new BufferedReader(new FileReader(file));
This just sets up the file that you will be reading, which then allows you to use a "while-loop" to read one line at a time using br.readLine().
String [] subtext = splitTokens(text,",");
This splits each line into bits, using a "," as a separator, and puts it into the subtext array.
columnOne.add(int(subtext[0]));
This shows how you can extract the first number or column from each line.
If you wanted the second number or column, you would use subtext[1] instead.
sensorData.add(text);
This shows how you can extract the ENTIRE line.
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
This is just error handling related to file reading.
br.close();
This closes the file.
for (int i=0; i<sensorData.size()-1; i++){
print(columnOne.get(i) + ".....");
println(sensorData.get(i));
}
This just prints the data to your screen so that you can see if the whole process has been successful.
Update : If you want to read values from a text file and then send these values to the Arduino - read this blog entry.
Description: Reading a text or CSV file using the Processing language Rating: 3.5 Reviewer: Unknown ItemReviewed: Reading a text or CSV file using the Processing language
0 comments:
Post a Comment