我正在尝试使用textscan
功能。以下是我试图阅读的数据:
"0", "6/23/2015 12:21:59 PM", "93.161", "95.911","94.515","95.917", "-5511.105","94.324","-1415.849","2.376","2.479"
"1", "6/23/2015 12:22:02 PM", "97.514", "96.068","94.727","96.138","-12500.000","94.540","-8094.912","2.386","2.479"
我使用的数据记录器会在所有值周围加上引号,即使它们是数字。如果用逗号分隔,我可以使用csvread
。你可以看到我注释失败的一些尝试。这是我一直在尝试的代码:
fileID = fopen('test3.txt');
%C = textscan(fileID,'"%f%s%f%f%f%f%f%f%f%f%f"', 'delimiter', '","');
C = textscan(fileID,'"%f","%s","%f","%f","%f","%f","%f","%f","%f","%f","%f"');
%C = textscan(fileID,'%s', 'delimiter', '"');
%C = strread(fileID, "%s %s %f %f %f %f %f %f %f %f %f", ",");
fclose(fileID);
celldisp(C)
如果我跑第3行,我会得到:
C{1} =
NaN
NaN
94.324
NaN
... omitted lines here ...
NaN
99.546
NaN
如果我执行第4,5或6行,我会得到:
warning: strread: unable to parse text or file with given format string
warning: called from
strread at line 688 column 7
textscan at line 318 column 8
test2 at line 4 column 3
error: some elements undefined in return list
error: called from
textscan at line 318 column 8
test2 at line 4 column 3
答案 0 :(得分:0)
你想要这个神奇的词。这个神奇的词不在这里,它的multipledelimsasone
。
基本上,您希望将"
和,
都视为分隔符。 textscan
会查找任何分隔符字符,而不是给定的顺序,这就是为什么'","'没有做你期望的事。启用multipledelimsasone
会使textscan
将"
和,
的任意组合视为单个分隔符。
C = textscan(fileID,'%f%s%f%f%f%f%f%f%f%f%f', 'delimiter', '," ','multipledelimsasone',1);
如果没有启用此选项,textscan
认为正在发生的事情就是很多空值;分隔符列表不是任何类型的顺序,只是可能的分隔符列表。因此,如果它看到","
,则认为您有三个分隔符,其间没有任何内容→两个空值→NaN
。