简体   繁体   English

Matlab TextScan FormatSpec分隔符错误

[英]Matlab textscan formatspec delimiter error

When reading a large csv file Matlab doesn't recognize ||,|| 当读取较大的csv文件时,Matlab无法识别||,|| as a proper delimiter as input argument for textscan. 作为适当的定界符,作为textscan的输入参数。 The data is as follows (simplified): 数据如下(简化):

||X||,||Y||,||Z|| (header)
||1||,||2||,||4||
||4||,||4||,||3||

etc. 等等

I use data = textscan(fileID,formatSpec,'Delimiter',','); 我使用data = textscan(fileID,formatSpec,'Delimiter',','); to read in the data with some format spec '%f %f %f' . 以某种格式'%f %f %f'读取数据。

My rubber band solution has been to use 010 editor to replace all '||' 我的橡皮筋解决方案是使用010编辑器替换所有“ ||” with '', making it a proper csv file for matlab, but due to the size of the document (6M lines with approx 35 fields) and the frequency of new documents this is hardly a great solution. 使用'',使其成为适用于Matlab的适当的csv文件,但是由于文档的大小(6M行和大约35个字段)以及新文档的出现频率,这并不是一个很好的解决方案。

Does anyone know a proper way to import such a file? 有谁知道导入此类文件的正确方法?

You should be able to include it in the format specifier: 您应该可以将其包含在格式说明符中:

data = textscan(fid, '||%f||,||%f||,||%f||', 'headerlines', 1)

and then just leave out the delimiter. 然后忽略定界符。

Edit (Following on from comments) 编辑 (从评论开始)

If you are trying to read in strings, the trick is to get it to read in strings without the | 如果您尝试读取字符串,诀窍是让它读取不带|字符串| character. 字符。 This is done using %[^|] , like this: 使用%[^|]完成此操作,如下所示:

data = textscan(fid, '|| %[^|] ||,|| %[^|] ||,|| %[^|] ||', 'headerlines', 1)

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

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