我正在使用以下命令来读取csv文件:
fid=fopen('test.csv');
scannedData = textscan(fid, '%4.0u%2.0u%2.0u%2.0u%2.0u%2.0u,%u,%u,%q,%q,%f,%f,%.2f,%u','whitespace','"');
fclose(fid);
问题是,文本扫描不会从最后一个字段读取值,而是在1行后停止。跳过该字段,为其分配不同的类型,在文本扫描中使用大量的eof组合,没有任何帮助。
文件中的数据如下所示:
"20100324072328","501","1","str1","str2","4.6846712","52.0159507","1.250000","128.000000"
"20100324072519","501","1","str1","str2","4.6846122","52.0159346","0.000000","128.000000"
"20100324072640","501","1","str1","str2","4.6846014","52.0159453","0.000000","128.000000"
"20100324072812","501","1","str1","str2","4.6845907","52.0159507","0.000000","96.000000"
"20100324073002","501","1","str1","str2","4.6845800","52.0159614","0.000000","128.000000"
当我尝试使用上述命令时,我想直接用文本扫描解析第一个文件。
我不想使用%q
读取字段的替代方法,然后解析生成的数组。
所以,我很感激有任何建议让文本可以一次完成。
感谢。
答案 0 :(得分:1)
如果您想将"
视为空格,那么您不应该使用需要双引号的%q
来标识完整字符串,如果您认为它们是空格,则无法找到它们:
fid = fopen('test.txt');
fmt = '%4u%2u%2u%2u%2u%2u%u%u%s%s%f%f%f%u';
out = textscan(fid,fmt,'Delimiter',',','Whitespace','"')
fclose(fid)
或者我建议在评论中使用:
fmt = '"%4u%2u%2u%2u%2u%2u" "%u" "%u"%q%q"%f" "%f" "%f" "%u"';
out = textscan(fid,fmt,'Delimiter',',')
请注意我的空格" "
,否则textscan()
无法识别字段何时结束。
但是,我个人可能会明确将日期转换为序列日期
fmt = '%s%u%u%s%s%f%f%f%u';
out = textscan(fid,fmt,'Delimiter',',','Whitespace','"')
out{1} = datenum(out{1},'yyyymmddHHMMSS');