Import location and velocity data from txt

edited November 2016 in Library Questions

Hello, I have a txt file that contains the location and velocity data for particles and I am trying to import it in processing but I have problem with the syntax. Here is an example of my txt file:

0.671470046043 0.0592122897506 -0.597842812538 0.165664941072 2.11239433289 0.165294915438,0.734478414059 0.0503597855568 -0.534131884575 0.207550317049 1.82068312168 -0.327298223972,0.784207165241 0.0618093051016 -0.443658143282 0.095640823245 2.19577264786 -0.197497576475

which is actually loc1.x loc1.y loc1.z vel1.x vel1.y vel1.z, loc2.x loc2.y loc2.z vel2.x vel2.y vel2.z, .....

here is the code that I am trying to write

  void importPts() {
    String[] pointsA = loadStrings(loadPts);
    importParticles = new Particle[pointsA.length];    
    for (int i = 0; i < pointsA.length; i++) {
      String a = pointsA[i];
      String[] a1 = split(a, ',');                             
      float x1 = float(a1[0]);
      float y1 = float(a1[1]);
      float z1 = float(a1[2]);
      float v1 = float(a1[3]);
      float v2 = float(a1[4]);
      float v3 = float(a1[5]);
      Vec3D loc1 = new Vec3D(x1, y1, z1);
      Vec3D vel1 = new Vec3D(v1,v2,v3);
      Particle pt = new Particle(loc1,vel1);
      importParticles[i]=pt;
    }
  }

for some reason I don't seem to be getting any results. Any idea of what I'm doing wrong?

thank you for the help!

Tagged:

Answers

  • This may not help you, but is this data written to the file by your program or some other program?

  • This may not help you, but is this data written to the file by your program or some other program? Or , to put it more directly, can you edit the program that writes the file in the first case? (P.S. If the file was typed in manually, then I have a better solution)

  • Answer ✓

    @leonidasarch===

    i can see 2 problems with your code, supposing that i have guessed correctly what is not present (your Particle class):

    • you are creating an array from your .txt: its length at this moment will be (and stay!) == 1; so, in the best case you will create only one particle at the end of your loop

    • then you split the result with (,); ok= what will be created by that is the first string segment, that is to say your six initial values

    • then you try to acceed to each value Without splitting the first segment

    • then you cast the result as a float:: but it CANNOT be a float.... whazt it returns is NaN!

  • yeah, it looks like your data is all in one line. Not so good. Or did you just post it wrongly? Try mark it as code with ctrl-o.

    what does println (pointsA.length); give you?

  • edited November 2016

    @Chrisir=== its all in one line (why not?) with ',' as split separator but (i have explained to him, perhaps too quickly) when it creates its array, before splitting, length can be only==1....

  • I see.

    comma is ok of course.

  • Thank you for all the replies! Yes, I realised that the txt file format is not great so I generated it again with the data of every particle being in a separate line and then I made some changes to the code and seems to work fine now! Here is the updated code

    void importPts() {
        String[] pointsA = loadStrings(loadPts);
        importParticles = new Particle[pointsA.length];    
        for (int i = 0; i < pointsA.length; i++) {
          String a = pointsA[i];
          String[] a1 = split(a, " ");                             
          float x1 = float(a1[0]);
          float y1 = float(a1[1]);
          float z1 = float(a1[2]);
          float v1 = float(a1[3]);
          float v2 = float(a1[4]);
          float v3 = float(a1[5]);
          Vec3D loc1 = new Vec3D(x1, y1, z1);
          Vec3D vel1 = new Vec3D(v1, v2, v3);
          Particle pt = new Particle(loc1, vel1);
          importParticles[i]=pt;
        }
      }
    
  • @leonidasarch===

    yes, that is a workaround; but supposing that you are loading values from some data bank (JSon) you probably not have any time to do so...I have seen that, writing code for an app (android) which uses maps and geoloc: i have more than 20 000 values for lat and long... So...

  • @leonidasarch -- glad it worked out!

    If you specify your headers (loc1.x loc1.y loc1.z vel1.x etc.) in the first row of a CSV file, you might try using loadTable() to import your data and work with it as a Table object -- there are a lot of nice convenience features.

  • edited November 2016

    The best would be to create your own file format of course, but that will require a lot of extra knowledge. I would not advise using a line containing a String for each data piece, especially when you use floats.This makes that file size stupidly large and if you have something like a 1000 different objects, the file would be about 10-15 times larger than required.

Sign In or Register to comment.