Search code examples
octaveros

Reading a csv file with spaces into Octave


I have some Pose Update rostopic data that I wrote onto a csv file using rostopic echo /foo > foo.csv

foo.csv looks like:

%time,field
1539675906586600065,0.157465848996
1539675906587352037,0.160723703902
1539675906587656974,0.161057770877
1539675906587693929,0.161579636574

I'm trying to import this into Octave for further processing but it's not exactly a constant delimiter and I have had no luck with dlmread or dlmwrite.

I tried

nsec = dlmread (nsecval,"\n",r1,c1)

 error: 'nsecval' undefined near line 1 column 17 error: evaluating argument list element number 1`

I need to:

  1. Ignore the first row with %time, field
  2. Take only the second part of each row e.g. 0.157465848996
  3. Convert the spaces into a format that dlmread or csvread can work with
  4. Convert all of those values into a column of a matrix.

E.g.

0.157465848996
0.160723703902
0.161057770877
0.161579636574

I'm really new to Octave and scientific computing in general and would really appreciate some help.


Solution

  • If you are using linux, I recommend a small preprocessing with sed to replace all commas with space:

     sed  -i "s#,# #g"  your_file
    

    If you are using windows or anything else, replacing the commas should be equally easy. Once you have this, in octave, just use

    i=load("your_file"); 
    vec=i(:,2);
    

    to get the second column into a column vector.