我正在使用textread
函数在Matlab中读取一个csv文件,并将值存储在字符串和浮点类型的单元格中。
[string1, string2, values] = textread('/path/xyz.csv', '%s %s %f', 'headerlines', 1);
数据有三列。我相信其中两个是字符串类型,一个是浮点型。
样本数据
@timestamp host value
March 5th 2019, 13:41:54.879 tscompute1 0.399
March 5th 2019, 13:41:54.879 tscompute1 0.599
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0.2
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 0
March 5th 2019, 13:41:54.879 tscompute1 100
March 5th 2019, 13:41:54.879 tscompute1 0.4
没有执行错误。但是读取的值不符合预期。请在下面找到示例输出。
存储在string1中的值如下所示
'"March'
','
'"March'
','
'"March'
','
'"March'
','
存储在string2中的值如下所示
'5th'
'13:41:54.879",tscompute1,0.399'
'5th'
'13:41:54.879",tscompute1,0.599'
'5th'
'13:41:54.879",tscompute1,0'
'5th'
'13:41:54.879",tscompute1,0.2'
存储在值中的值如下所示
2019
0
2019
0
2019
0
2019
0
答案 0 :(得分:1)
您的文本似乎有不一致的定界符,日期与时间之间用逗号分隔,而时间,名称“ tscompute1”和数字由空格分隔。
最简单的方法是将每一行读为六个元素,每个元素之间用空格隔开,其中五个为字符串,第六个为数字。
[s1, s2, s3, s4, s5, values] = textread('/path/xyz.csv', '%s %s %s %s %s %f', 'headerlines', 1);
这使您可以获取日期(在s1-s3中连接字符串,删除结尾的逗号),时间(s4),名称(s5)和值。