是否可以在 Scilab 5.5.2 中使用 csvRead 设置可变范围和列

Is it possible to have a variable range and columns using csvRead in Scilab 5.5.2

我是一个使用 Scilab 的自学成才的新手程序员。我有想要阅读的 .csv 文件。它们是混合文本和数值,并且具有可变数量的列和行。我感兴趣的文件部分有固定数量的列,但没有行。我可以使用 header 参数跳过第一部分,但底部也有我不需要的单元格。它的外观示例:

DATA,1,0,3,3960.4,3236,3373,-132
DATA,1,0,4,4544.5,3530,3588,-76
RANDOM TEXT,0
INFO,1,0,#+BHO0 _:WRF&-11,S%00-0-03-1       
INFO,2,1,#*BHO0 _8WRF&-11,NAS%00-0-15-1

我只对以 DATA 开头的行感兴趣。如果我尝试 运行 csvRead 而不删除下面的行,我会收到此错误:

Warning: Inconsistency found in the columns. At line 4993, found 2 columns 
while the previous had 8.

我目前有一个程序可以读取文件并根据需要对其进行操作,但我必须进入每个文件并删除底部的行。有办法解决这个问题吗?

我当前的程序看起来像这样:

D = uigetfile([".csv"],"path", "Choose a file name", %t);
filename = fullfile(D);
sub = ["DATA" "0"];

//Import data
data = csvRead(filename, ',', [], 'string', sub, [], [], 34);
edit(filename)
//determine # of rows
data_size = size(data);
limit = data_size(1);

有什么想法吗?

不可能指定 csvRead 应该忽略列较少的行,或者使用默认集或任何东西(这会很好)。 解决方法可能适用于您的情况,只解析以 DATA 开头的行。这可以通过 Regular Expressions.

来完成

csvReadregexpcomments 参数提供了忽略 csv 文件中与特定正则表达式匹配的行的机会。除此之外,还可以编写一个正则表达式来匹配所有匹配特定模式的字符串:

/^(?:(?!PATTERN).)*$/;    # Matches strings not containing PATTERN

在你的情况下应用这个正则表达式,会导致所有 not 包含 PATTERN 的行都被假定为注释,因此将被忽略。

在代码中,意思如下。

filename = fullfile('data.csv');
sub = ["DATA" "0"];

//Import data
number_of_header_lines = 1
read_only_lines_starting_with = 'DATA'
regexp_magic = '/^(?:(?!' + read_only_lines_starting_with + ').)*$/'

data = csvRead(filename, ',', [], 'string', sub, regexp_magic, [], number_of_header_lines);

disp(data)