saving data/variables in processing

edited January 2017 in How To...

Hello everyone

In my program im using several variables and I want to save them to txt file or excel file. After that I will make some graphs with them in other program. http://scr.hu/5o3i/35y8h here is my variables. I know how to show them in console by println but idk how to save them. Saw saveTable and saveStrings but i dont know how to use them to my program.

thx for help

Answers

  • Saw this but ive got float values and i think i cant save it by strings, am I right ?

  • @merbb --

    ive got float values and i think i cant save it by strings

    Well, a .txt or .csv file is string character data. So if you want to store floats in that format then you have to save them as strings at some point. However, Table will handle this for you.

    Look at the Table reference page, which includes getFloat and setFloat methods:

    Then try the example sketch for setFloat -- and try adapting the saveTable and loadTable example sketches from the reference to include a float column to see how this works.

  •   table = new Table();
    
      table.addColumn("xp", Table.FLOAT);
      table.addColumn("yp", Table.FLOAT);
      table.addColumn("zp", Table.FLOAT);
    
      table.addRow();  // Creates an empty row
    
      table.setFloat(0, "xp", xp);
      table.setFloat(0, "yp", yp);
      table.setFloat(0, "zp", zp);
    
    
      for (int i = 0; i < 1000; i = i+1 ) {
        table.addRow();
      }
    
      println(table.getFloat(0, "xp"));   
      println(table.getFloat(0, "yp"));   
      println(table.getFloat(0, "zp")); 
    
      saveTable(table, "data/new.csv");
    

    There is one problem in my table. It saves data in file only in 1st row. Next are 0's. In console it saves every single one http://scr.hu/5o3i/b80nm but in file it's like here http://scr.hu/5o3i/uo5zk . Other question: why ive got xp, yp, zp in 1 column ? There are 3 columns in my sketch.

  • @GoToLoop

    http://scr.hu/5o3i/rnabp. This is how it works but its still bad i want sth like this http://scr.hu/5o3i/xewuu

  • In line 14-16 you're just adding a thousand empty rows to the table.

  • Sorry, but why did you post a non-compilable code anyways? :-@

  • edited January 2017

    I understand, I've just wanted to make more rows for xp,yp,zp. Still dont know how to save every single one 1 by 1 in table

    @GoToLoop Its only few line from the whole code. I cant upload it because its about 600 lines. Need just some answers and i will do it.

  • It's only saving the last position of robot but i want to save every position to make some graphs

  • Move lines 7 to 11 into your for loop in line 15 and it should work. Here is another approach https://forum.processing.org/two/discussion/comment/72439/#Comment_72439 where it shows how to access your rows that you just created suing your current code.

    Kf

  • Unfortunately it doesn't work. http://scr.hu/5o3i/ot9tz

  • Still you should try to post a runnable code to give some idea what's exactly about it.
    The way it is now, it just sends the wrong signal, given it simply creates 1001 empty rows. :-@

  • edited January 2017
    Table table;
    float xp = 0;
    float yp = 0;
    float zp = 0;
    
    void draw() {
    
      table = new Table();
    
      table.addColumn("xp", Table.FLOAT);
      table.addColumn("yp", Table.FLOAT);
      table.addColumn("zp", Table.FLOAT);
    
      for (int i = 0; i < 1000; i = i+1 ) {
        table.addRow();  // Creates an empty row
    
        table.setFloat(0, "xp", xp);
        table.setFloat(0, "yp", yp);
        table.setFloat(0, "zp", zp);
      }
    
      println(table.getFloat(0, "xp"));   
      println(table.getFloat(0, "yp"));   
      println(table.getFloat(0, "zp")); 
    
      saveTable(table, "data/new.csv");
    }
    

    It works like this:

    1. Im moving my mouse and xp and yp are changing in time.
    2. Im moving a slider zp and zp is changing
    3. in console every position of xp,yp, zp is saving but in file ive got only last position of my xp,yp,zp saved in 1st row.

    I want to save every position of xp, yp, zp in a different rows.

    Do u know what I need now ?

  • Answer ✓

    This code saves a fixed number of entries of mouseX and mouseY data.

    Kf

    final int MAXDATA=100;
    int ctr=0;
    
    Table table;
    
    void setup() {
      size(400, 600);
      table = new Table();
    
      table.addColumn("xp", Table.FLOAT);
      table.addColumn("yp", Table.FLOAT);
      table.addColumn("zp", Table.FLOAT);
    
    
      frameRate(5);
      fill(255);
    }
    
    void draw() {
      background(0);
      text("Current count "+ctr+" out of "+MAXDATA, width/2, height/2);
    
      table.addRow();  // Creates an empty row
      table.setFloat(ctr, "xp", mouseX);
      table.setFloat(ctr, "yp", mouseY);
      table.setFloat(ctr, "zp", random(year()));  //Random data
      ctr++;
    
      if (ctr==MAXDATA) {
        saveTable(table, "data/new.csv");
        exit();
      }
    }
    

    Another way to save data. However it doesn't help in your case. Shown here just for demonstration:

    Table table;
    
    void setup(){
    
      table = new Table();
    
      table.addColumn("xp", Table.FLOAT);
      table.addColumn("yp", Table.FLOAT);
      table.addColumn("zp", Table.FLOAT);
    
      for (int i = 0; i < 1000; i = i+1 ) {
        table.addRow();  // Creates an empty row
    
        table.setFloat(i, "xp", day());
        table.setFloat(i, "yp", month());
        table.setFloat(i, "zp", random(year()));
      }
    
      saveTable(table, "data/new.csv");
    }
    

    Kf

  • edited January 2017

    Hell yeah @kfrajer TY very much, your answer is that I was looking for. Sorry for my English im speaking Polish normally

  • edited January 2017

    Oh, @kfrajer was much faster than moi! =P~
    Anywayz, gonna post mine as well since it's still an alternative way on how to do the same stuff: :ar!

    /**
     * Save Coords Table (v1.0)
     * GoToLoop (2017-Jan-17)
     *
     * forum.Processing.org/two/discussion/20305/
     * saving-data-variables-in-processing#Item_17
     */
    
    static final String[] HEADER = { "xp", "yp", "zp" };
    static final String FILENAME = "RobotCoords.csv";
    
    Table coords;
    Object[] xyz = new Object[HEADER.length];
    float xp, yp, zp;
    
    void setup() {
      size(300, 200);
      frameRate(1);
      coords = new Table();
      coords.setColumnTitles(HEADER);
      coords.setTableType("float");
    }
    
    void draw() {
      background((color) random(#000000));
      pseudoCoordGen();
      addNewCoordRow();
      getSurface().setTitle("# Rows: " + coords.getRowCount());
      println(xp, yp, zp);
    }
    
    void mousePressed() {
      saveTable(coords, dataPath(FILENAME));
      System.err.println("Coord Table Saved w/ # rows = " +
        coords.getRowCount());
    }
    
    void pseudoCoordGen() {
      xp = random(width);
      yp = random(height);
      zp = xp/yp;
    }
    
    void addNewCoordRow() {
      xyz[0] = xp;
      xyz[1] = yp;
      xyz[2] = zp;
      coords.addRow(xyz);
    }
    
  • @GoToLoop

    How would you modify your code to append data without reading the original file first, as I have seen in previous post? I am not sure if current Processing functions allow to do this. I have seen doing this using standard java output routines. Do you know a way of doing this?

    Kf

  • edited January 2017

    You've said it yourself: "I am not sure if current Processing functions allow to do this.".
    Unless the ".csv" becomes too huge, we shouldn't worry about its size.
    Otherwise, we should even drop Table and do everything in a boilerplate Java-ish way! :-SS

Sign In or Register to comment.