selectInput and graph

Hello all, I want to write a code to select a ".csv" file and graph the corresponding data. So far I've been able to do those separately but not together.

Table table;
String myFilePath;
TableRow row;
float Time;
float Manom;
float Temp;
float P1;
float GV1;
float P2;
float GV2;
float P3;
float GV3;
float P4;
float GV4;
float P5;
float GV5;
float P6;
float GV6;
float x;
int var;
Bubble[] bubbles;


void setup() {
  size(1000, 800);
  surface.setResizable(true);
  selectInput("Select a file to process:", "fileSelected");
  loadData();
}

void fileSelected(File selection) {
  myFilePath = selection.getAbsolutePath();
  table = loadTable(myFilePath, "header");
}

void draw(){
  if(table == null){
    background(0,0,155);
    noLoop();
  } else {
    background(155,50,50);
    for (int i = 0; i < bubbles.length; i++) {
    bubbles[i].display();
    }
  }
}

void loadData(){
  if (table != null){
    background(70);
    table = loadTable(myFilePath, "header");
    bubbles = new Bubble[table.getRowCount()]; 
    for (int i = 0; i < table.getRowCount(); i++){
      row = table.getRow(i);
      Time = row.getInt("Time (s)");
      Manom = row.getFloat("1");
      Temp = row.getFloat("2");
      P1 = row.getFloat("3");
      P2 = row.getFloat("4");
      P3 = row.getFloat("5");
      P4 = row.getFloat("6");
      P5 = row.getFloat("7");
      P6 = row.getFloat("8");
      GV1 = row.getFloat("9");
      GV2 = row.getFloat("10");
      GV3 = row.getFloat("11");
      GV4 = row.getFloat("12");
      GV5 = row.getFloat("13");
      GV6 = row.getFloat("14");
      x = map(Time, 0, table.getRowCount(), 100, 900);
    }
  }
}

Main code with class Bubble

class Bubble {
  float x,y;
  float diameter;


  Bubble(float x_, float y_, float diameter_) {
    x = x_;
    y = y_;
    diameter = diameter_;
  }


  void display() {
    stroke(0);
    strokeWeight(2);
    noFill();
    ellipse(x,y,diameter,diameter);
  }
}

running this code gives me this error

Untitled

Answers

  • Please post your entire code as text and not as an image

  • @Chrisir sorry, first time posting. Should look better now.

  • edited June 2017

    Call loadData only from fileselected and not from setup () --- setup doesn't wait!!!

    Don't use noLoop

  • Or use loadData from draw

  • Just tried putting loadData in several different spots including draw and fileselected and got the same error.

  • I cant read your screen shot. :-?? Knowing the error is half of the solution... Regarding inputSelect: It is better if you copy and paste your error here in the forum. You can call load data in setup but then you have to make sure you access the data only after the data has been loaded. Check the concept below.

    Kf

    boolean dataLoaded=false;
    
    void setup(){
       ...
       selectInput(...);
    }
    
    // SELECT input callback----> dataLoaded=true;
    
    
    void draw(){
       if(dataLoaded==false){
          return;
       }
    
       ...
       ...   DRAW functions here
       ...
    }
    
  • Answer ✓

    please show the first 5 lines of the table / csv

    here is my version

    Table table;
    
    String myFilePath;
    TableRow row;
    float Time;
    float Manom;
    float Temp;
    float P1;
    float GV1;
    float P2;
    float GV2;
    float P3;
    float GV3;
    float P4;
    float GV4;
    float P5;
    float GV5;
    float P6;
    float GV6;
    float x;
    int var;
    
    Bubble[] bubbles;
    
    // -----------------------------------------
    
    void setup() {
      size(1000, 800);
      surface.setResizable(true);
      selectInput("Select a file to display bubbles:", "fileSelected");
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
        exit();
        return;
      } 
      myFilePath = selection.getAbsolutePath();
      // table = loadTable(myFilePath, "header");
      loadData();
    }
    
    void draw() {
      if (table == null) {
        background(0, 0, 155);
        // wait
      } else {
        background(155, 50, 50);
        if (bubbles!=null) {
          for (int i = 0; i < bubbles.length; i++) {
            bubbles[i].display();
          }
        }
      }
    }
    
    void loadData() {
      if (table == null) {
    
        background(70);
    
        String [] dummy=loadStrings(myFilePath); 
        println(dummy[0]);
    
        table = loadTable(myFilePath, "header");
    
        if (table==null) {
          println("Error");
          exit ();
          return;
        }
    
    
        bubbles = new Bubble[table.getRowCount()]; 
        for (int i = 0; i < table.getRowCount(); i++) {
          row = table.getRow(i);
          Time = row.getInt("Time (s)");
          Manom = row.getFloat("1");
          Temp = row.getFloat("2");
          P1 = row.getFloat("3");
          P2 = row.getFloat("4");
          P3 = row.getFloat("5");
          P4 = row.getFloat("6");
          P5 = row.getFloat("7");
          P6 = row.getFloat("8");
          GV1 = row.getFloat("9");
          GV2 = row.getFloat("10");
          GV3 = row.getFloat("11");
          GV4 = row.getFloat("12");
          GV5 = row.getFloat("13");
          GV6 = row.getFloat("14");
          x = map(Time, 0, table.getRowCount(), 100, 900);
        }
      }
    }
    
    //=================================
    
    class Bubble {
      float x, y;
      float diameter;
    
    
      Bubble(float x_, float y_, float diameter_) {
        x = x_;
        y = y_;
        diameter = diameter_;
      }
    
    
      void display() {
        stroke(0);
        strokeWeight(2);
        noFill();
        ellipse(x, y, diameter, diameter);
      }
    }
    //
    
  • edited June 2017
    Time (s)    1   2   3   4   5   6   7   8   9   10  11  12  13  14
    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    3   -5  0   -50.5   0   -50.5   -100    -50.5   -100    -50.5   -100    -50.5   -100    -50.5   -100
    5   -4.310135   3.4304502   -9.862869   14.658532   -97.606125  -515.5752   -192.8912   -943.5985   -277.92416  -1264.3966  -351.54956  -1582.1349  -414.06622  -1885.5575
    7   -17.897194  -4.241104   -226.42467  14.706368   -367.74652  -1816.2155  -498.46567  -2389.1855  -585.02075  -2500.5803  -585.02466  -2500.5803  -585.02075  -2500.5803
    9   -20.650635  -4.719874   -268.5407   14.69308    -433.136    -2160.2588  -585.02075  -2500.5981  -585.02466  -2500.5981  -585.02466  -2500.5981  -585.02075  -2500.5803
    
  • Does my sketch work for you?

  • I got a NullPointerException error and it printed the header row

  • @kfrajer this is the error I was getting with my first code.

    java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at processing.core.PApplet.selectCallback(PApplet.java:6543) at processing.core.PApplet.access$1(PApplet.java:6536) at processing.core.PApplet$3.run(PApplet.java:6447) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Caused by: java.lang.IllegalArgumentException: No extension specified for this Table at processing.data.Table.parse(Table.java:369) at processing.data.Table.(Table.java:154) at processing.core.PApplet.loadTable(PApplet.java:6071) at processing.fileSelected(processing.java:49) ... 21 more

  • So does my sketch work ?

    Try to fix my sketch please for your needs

  • @Chrisir your fixes took it from the error above to a NullPointerException and highlighted bubbles[i].display() under draw. both ways work until after I choose a file.

  • Does my sketch print the first line of the file?

  • Try another newly made file please

  • Answer ✓

    Strange

    Oh, wait I think you forgot to fill bubbles

    You need after line 93

    bubbles[i] = new Bubble(......

  • edited June 2017

    Not sure what 81 to 92 do

    You overwrite the values all the time

    Shouldn't all those variables be part of the class?

  • @Chrisir it works! I just need to do some tweaking. Rows 81-92 call all the values in a corresponding column.

  • In the error I see:

    No extension specified for this Table at processing.data.Table.parse

    I will suggest to skip the selectinput() function and load the table manually by specifying the extension and see if that solve this part of the program. Or you can use the option parameters while loading tables: https://processing.org/reference/loadTable_.html

    Notice also that in the data sample you provided, you have 15 columns in your header but you have this: "Time (s)" and this is not good as this will be counted as two field. Change it to either "Time(s)" or just "Time".

    Kf

  • The two posts overlapped I guess

    It's solved, it was the missing filling of bubbles

    We looked in the wrong place

Sign In or Register to comment.