Thursday, June 23, 2011

Reading a text or CSV file using the Processing language

In a previous post, I showed you how to export data to a text file. Now I will show you how to import it back into your Processing program. This will come in handy later on.

This is what my data looks like in the text file:

There are many ways to import text from a text file, and there are many ways to store the data within your code after you have imported it. Feel free to make mention of your method in the comments, however, this one definitely works, and is doing what I want it to do.

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

/*==========================================================
Project: Read data from Text file into Processing
Author: ScottC
Created: 23rd Jun 2011 (Updated 26th Aug 2014)
Description: Use processing to read a text file and populate an ArrayList.
             The ArrayList is then printed to the debug window.
             
Processing version tested: 2.2.1
References: This was made possible using bits and pieces from these sites
         http://www.kodejava.org/examples/28.html
         http://processing.org/reference/ArrayList.html
         http://processing.org/reference/splitTokens_.html
         
===========================================================  */
import java.io.FileReader;
import java.io.FileNotFoundException;

ArrayList sensorData;
ArrayList columnOne;

void setup(){
  sensorData=new ArrayList();
  columnOne=new ArrayList();
  readData("C:/SensorData/mySensorData.txt");
}

void readData(String myFileName){
  
  File file=new File(myFileName);
  BufferedReader br=null;
  
  try{
    br=new BufferedReader(new FileReader(file));
    String text=null;
    
    while((text=br.readLine())!=null){
      String [] subtext = splitTokens(text,",");
      columnOne.add(int(subtext[0]));
      sensorData.add(text);
    }
  }catch(FileNotFoundException e){
    e.printStackTrace();
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    try {
      if (br != null){
        br.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  for (int i=0; i<sensorData.size()-1; i++){
    print(columnOne.get(i) + ".....");
    println(sensorData.get(i));
  }
}

 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.









No comments:

Post a Comment