简体   繁体   中英

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 eg 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.

Eg

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.

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.

Firstly, the tried issue with nsec = dlmread (nsecval,"\\n",r1,c1):

The octave error is telling you that it doesn't know what 'nsecval' is.

Next using dlmread, the function is expecting the filename, the delimiter value, start row, start column (from 0) so something like:

nsec = dlmread ("foo.csv", ",", 1, 1)

will get the data you require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM