use variable with the method loadTable();

I want to make String filename as a variable in loadTable(String filename, option), so each time when the variable change, the system will reload a new table. but it seems it doesn't work.

my code:

Table table; String testing = "data.csv"; //this file name could be changed, I used a String array to save all the csv file name

void draw(){

table=loadTable(testing);

}

Tagged:

Answers

  • Do not load table in draw() ! when you're doing it, program tries to load table each frame, this slows everything and each frame all changes made to table would be lost. If you want to load table during the runtime, you need to create a function, that would be executed only once. Here it is explained how to do it.

  • Thank you for your reply. But can I use variables to change the filename? it seems sometimes it work, but sometimes I just got an error message " error reading table on line xxx", but I dont know what is the error. can anyone give me some hints?

  • Answer ✓
    1. Yes you can use a string variable anywhere where string is expected.
    2. Why would you want to use a variable? You want to reassign another table to the same variable? But you need to call loadTable() separately anyway. As I said you can't use loadTable() in draw(), I'm guessing problems you have are because you didn't change that.
  • edited October 2015

    thank you, i tried to create loadData() method and then use it in the if statement in draw(), and again I have the same error message --"error reading table on line xxx" ...

    Here is my code:

    int [][]randomEnt=new int[x+1000][y+1000]; HashSet<Integer> xCount = new HashSet<Integer>(); HashSet<Integer> yCount = new HashSet<Integer>(); void loadData(String s){ Table table1 = loadTable(s, "header"); println(table1.getRowCount() + " total rows in table"); for (TableRow row : table.rows()) { x = row.getInt("x"); y = row.getInt("y"); ent = (int)row.getFloat("ent")*100; xCount.add(row.getInt("x")); yCount.add(row.getInt("y")); randomEnt[(row.getInt("x"))][(row.getInt("y"))]=int(row.getFloat("ent")*100); } }

  • Don't you have same error if you just load same table in setup()? The code looks OK, but once again, if you use this method in draw it can be executed more then one time, you need to set boolean dataLoad, in draw use if(dataLoad){//yor method} and set dataLoad = false; right in the end of the method, it will guarantee that method executes only one time.

  • Hi, I am using P5 control to change the String s in the loadData() method mentioned above. and when I put it in the setup() I got error message like this: Screen Shot 2015-10-21 at 09.47.48

Sign In or Register to comment.