Can't read y-axis of loadString

I can't get it to read the coordinates for the y-axis. xy consists of these numbers: -13.3,-16.39,-5.51,-2.96,4.65,9.0,11.97,10.57,6.09,-0.35,-5.52,-15.84 Could i get a hint to what i'm doing wrong? And why didn't Float.parseFloat work? Thank you!

//float[] tall = {-13.3,-16.39,-5.51,-2.96,4.65,9.0,11.97,10.57,6.09,-0.35,-5.52,-15.84};
String[] line = loadStrings("data/xy.csv");
String[] list = split(line[0], ',');
//float f =  Float.parseFloat(line);

size(1000, 800);

  for(int i = 0; i < line.length; i++) {
    float xpos = map(i, 0, line.length, 50, width - 100);
    float ypos = map(list[i], -5, 40, height, 0); 
    stroke(0);
    fill(255);
    rect(xpos, ypos, 70, 1);//Array
    fill(0);
    rect(0,266,930,1);//x-akse
    rect(30,0,1,600);//y-akse
    stroke(255);

  }

Comments

  • Float.parseFloat(line) did not work because line is an array and this method expects a single String.

    You might try changing line 10 to

    float ypos = map(Float.parseFloat(list[i]), -5, 40, height, 0);
    

    I don't have the time right now to test your code but I hope this helps.

  • I tried, but it still won't display any data. Basically I'm trying to create a histogram, but i think I might be doing it wrong.

  • Some errors:

    • size() must be the first line of setup().
    • I hope the loadStrings() line is inside setup(), not at the global level (or is the whole program in immediate mode?)
    • loadStrings() already loads files from the data folder, so the "data/" part is redundant, at best.
    • As said, Float.parseFloat() expects a single string. But Processing offers the float() function doing the same thing, but also accepting an array of strings, so better use this one.
  • I guess I had it in immediate mode, but here is an edited version. But how can i get the float() function to get data from loadStrings()? Thanks for the help so far, but I'm pretty new to this!

    //float[] tall = {-13.3,-16.39,-5.51,-2.96,4.65,9.0,11.97,10.57,6.09,-0.35,-5.52,-15.84};
    
    void setup(){
    size(1000, 500);
    String[] line = loadStrings("vaer.csv");
    String[] list = split(line[0], ',');
    
    
      for(int i = 0; i < line.length; i++) {
        float xpos = map(i, 0, line.length, 50, width - 100);
        float ypos = map(Float.parseFloat(list[i]),50, 50, height, 70);
        rect(xpos, ypos, xpos+100, ypos+100);//Array
      }
    }
    void draw(){
    
     stroke(0);
        fill(255);
        //rect(xpos, ypos, xpos+100, ypos+100);//Array
        fill(0);
        rect(0,266,930,1);//x-akse
        rect(30,0,1,600);//y-akse
        stroke(255);
    
      }
    
  • edited October 2013

    Perhaps this 1 is close to whatcha want? 8-X

    /** 
     * FloatList Example (v1.0)
     * by GoToLoop (2013/Oct)
     * 
     * forum.processing.org/two/discussion/404/cant-read-y-axis-of-loadstring
     */
    
    //final String[] lines = loadStrings("vaer.csv");
    
    final String[] lines = {
      "-13.3, -16.39, -5.51\n", "-2.96, 4.65, 9, 11.97\n", 
      "10.57, 6.09, -.35, -5.52, -15.84\n"
    };
    
    final FloatList data = new FloatList();
    
    for ( String s: lines )   for ( float f: float(split(s, ',')) )   data.append(f);
    
    println(data);
    exit();
    
  • edited October 2013

    Thank you guys! That last example worked after i added it to my own code. I sat grinding my teeth pretty long before I asked the forum, so you saved me a trip to the dentist ^^

Sign In or Register to comment.