简体   繁体   English

将带空格的csv文件读入Octave

[英]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 我有一些使用rostopic echo / foo> foo.csv写入到csv文件中的Pose Update rostopic数据

foo.csv looks like: foo.csv看起来像:

%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. 我正在尝试将其导入Octave进行进一步处理,但这并不是一个恒定的定界符,并且我对dlmread或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 忽略带有%time字段的第一行
  2. Take only the second part of each row eg 0.157465848996 仅取每行的第二部分,例如0.157465848996
  3. Convert the spaces into a format that dlmread or csvread can work with 将空格转换为dlmread或csvread可以使用的格式
  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. 总的来说,我真的很熟悉Octave和科学计算,并希望获得一些帮助。

If you are using linux, I recommend a small preprocessing with sed to replace all commas with space: 如果您使用的是Linux,我建议使用sed进行小的预处理,以用空格替换所有逗号:

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

If you are using windows or anything else, replacing the commas should be equally easy. 如果您使用的是Windows或其他工具,则替换逗号同样应该很容易。 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): 首先,尝试使用nsec = dlmread(nsecval,“ \\ n”,r1,c1)来解决问题:

The octave error is telling you that it doesn't know what 'nsecval' is. 八度错误告诉您它不知道什么是“ nsecval”。

Next using dlmread, the function is expecting the filename, the delimiter value, start row, start column (from 0) so something like: 接下来使用dlmread,该函数期望文件名,定界符值,起始行,起始列(从0开始),如下所示:

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

will get the data you require. 将获取您所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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