gpt4 book ai didi

matlab - 在 Octave/MATLAB 中读取 csv 文件时可以忽略注释行吗?

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:13 25 4
gpt4 key购买 nike

我有一个如下所示的数据文件:

# 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

我想用 Octave 程序阅读它。

csvread(file,3,0) 在这种情况下工作得很好,但我担心必须手动计算出 3。

有什么方法可以说“在执行 csvread 之前丢弃任何以 # 开头的行和任何空行”?

最佳答案

在 Octave 内你可以做

d = load("yourfile")

应该忽略 # 行

编辑:上面使用了文件类型的自动检测,您也可以使用 d = load ("-ascii", "yourfile") 强制它。 引用自 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.

不幸的是,帮助没有提到以 % 或 # 开头的行将被忽略。为此,您必须查看源代码(幸运的是,它是可用的,因为 GNU Octave 是免费软件)get_mat_data_input_line from octave source

从那里您可以看到 % 或 # 之后的所有字符都被跳过了。

关于matlab - 在 Octave/MATLAB 中读取 csv 文件时可以忽略注释行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26206513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com