Text file reading

edited July 2017 in How To...

I have a txt file in which point cloud data are stored..i want to use that data array..and after editing i want to draw that point cloud data..can anyone please provide me the code for this?????

Answers

  • edited July 2017

    ...and saveString()... and split()....

    OR

    or via loadTable and saveTable

    can you post the first 10 lines of the file?

    the tricky part is editing. Do you mean text editing ? Or visual editing?

  • edited July 2017

    simple version with loading and editing, but without saving

    make a txt file in the sketch folder named "data.csv" with the content as shown below (without the leading space in each line!)

    // shows a Table 
    
    /*
    
     example table - make a txt file in the sketch folder named 
     "data.csv" with this content: 
    
     x,y,diameter,name
     112,122,22,Boy 
     212,222,32,Girl 
     312,322,12,Fred
    
    
     */
    
    DataEntry[] dataList;
    boolean hold=false; 
    DataEntry holdItem;
    
    void setup() {
      size(480, 360);
      loadData();
    }
    
    void draw() {
      background(255);
    
      // message upper right corner 
      fill(0); 
      textAlign(LEFT);
      text("Showing data from text file; use mouse to drag items", 14, 14);
    
      // show words 
      drawWords();
    
      // for dragging: 
      if (hold && holdItem!=null) {
        holdItem.x += mouseX-pmouseX; 
        holdItem.y += mouseY-pmouseY;
      }
    }
    
    // -------------------------------------------------------
    
    void drawWords() {
      for (DataEntry item : dataList) 
        item.display();
    }
    
    void loadData() {
    
      Table table;
    
      // "header" indicates the file has header row. The size of the array 
      // is then determined by the number of rows in the table.
    
      table = loadTable("data.csv", "header");
    
      dataList = new DataEntry[table.getRowCount()];
    
      for (int i = 0; i<table.getRowCount(); i++) {
        // Iterate over all the rows in a table.
        TableRow row = table.getRow(i);
    
        // Access the fields via their column name (or index).
        float x = row.getFloat("x");
        float y = row.getFloat("y");
        float d = row.getFloat("diameter");
        String n = row.getString("name");
        // Make a DataEntry object out of the data from each row.
        dataList[i] = new DataEntry(x, y, d, n);
      }
    }
    
    // -----------------------------------------------------------
    
    void mousePressed() {
      // start dragging 
      hold=true; 
      for (DataEntry item : dataList) 
        if (item.over()) {
          holdItem=item;
          return;
        }
    }
    
    void mouseReleased() {
      // stop dragging 
      // reset 
      hold=false;
      holdItem=null;
    }
    
    // ========================================================================
    
    // This simple DataEntry class draws a circle to the window 
    // and displays a text label 
    class DataEntry {
    
      float x, y;
      float diameter;
      String name;
    
      // boolean over = false;
    
      // Create the DataEntry (constructor)
      DataEntry(
        float tempX, float tempY, 
        float tempD, 
        String s) {
        x = tempX;
        y = tempY;
        diameter = tempD;
        name = s;
      } // (constructor)
    
      // Display the DataEntry
      void display() {
        stroke(0);
        if (over())
          stroke(255, 0, 0);
        strokeWeight(2);
        noFill();
        ellipse(x, y, diameter, diameter);
        fill(0);
        textAlign(CENTER);
        text(name, x, y+diameter/2+20);
        textAlign(LEFT);
      }
    
      boolean over() {
        return 
          dist(mouseX, mouseY, x, y) < diameter+3;
      }
      //
    }//class
    //
    
  • edited July 2017

    loadStrings() is the simplest way of doing it yourself, but I'm a big fan of loadTable() -- it is a bit more verbose, but great for working with loading and saving structured data files. The reference entries for methods of loadTable have lots of examples of how to use it.

Sign In or Register to comment.