[英]Can I ignore comment lines while reading in a csv file in Octave/MATLAB?
I've got a data file that looks like: 我有一个看起来像这样的数据文件:
# data file
# blah
# blah
0.000000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.020000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.040000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.060000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.080000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.100000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
0.120000, 0.0, 24.198, 6.864,NaN,NaN,NaN,NaN
and I'd like to read it with an Octave program. 我想用Octave程序阅读它。
csvread(file,3,0) works perfectly in this case, but I worry about having to work out the 3 by hand. csvread(file,3,0)在这种情况下工作得很好,但是我担心必须手工完成3。
Is there some way to say 'throw away any lines starting with #, and any blank lines, before doing the csvread'? 有没有办法说'扔掉任何以#开头的行,以及任何空白行,然后再进行csvread'?
In octave you could do 八度,你可以做到
d = load("yourfile")
which should ignore the # lines 应该忽略#行
Edit: The above uses autodetection of the file type, you could also force it with d = load ("-ascii", "yourfile").
编辑:上面使用自动检测文件类型,你也可以用
d = load ("-ascii", "yourfile").
强制它d = load ("-ascii", "yourfile").
Quote from help load
: 从
help load
引用:
'-ascii'
Force Octave to assume the file contains columns of numbers in
text format without any header or other information. Data in
the file will be loaded as a single numeric matrix with the
name of the variable derived from the name of the file.
Unfortunately the help doesn't mention that lines starting with % or # are ignored. 不幸的是,帮助没有提到以%或#开头的行被忽略。 For this you have to look at the source code (which is fortunately available since GNU Octave is free software) get_mat_data_input_line from octave source
为此,你必须查看源代码(幸运的是,因为GNU Octave是自由软件,所以可以使用) 来自octave源的get_mat_data_input_line
From there you can see that all chars after % or # are skipped. 从那里你可以看到跳过%或#之后的所有字符。
csvread
does not allow this option. csvread
不允许此选项。 Instead, you can use textscan
, but then, you need to know how many columns (or rows) your csv file has. 相反,您可以使用
textscan
,但是,您需要知道csv文件有多少列(或行)。
For example: 例如:
fid = fopen('csvFile.csv','r');
c = textscan(fid,'%f','commentStyle','#','delimiter',',');
fclose(fid); %# close the file as soon as we don't need it anymore
array = reshape([c{:}],[],7)';
Here is a way to skip header lines starting with a comment string. 这是一种跳过以注释字符串开头的标题行的方法。 The
csvread
line could be replaced by a dlmread
call for delimiters other than ','
. csvread
行可以替换为除','
之外','
分隔符的dlmread
调用。 Both these functions are much faster than textscan
on octave 3.8.2. 这两个函数都比八度音程3.8.2上的
textscan
快得多。
fid = fopen('csvFile.csv','r');
comment = '#';
while strcmp(fgets(fid, length(comment)), comment)
% line begins with a comment, skip it
fskipl(fid);
endwhile
% get back, because the last read length(comment) characters
% are not comments, actually
fseek(fid, -length(comment), SEEK_CUR);
c = csvread(fid);
fclose(fid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.