我习惯在scilab中使用csvRead读取我的数据文件,但是现在我面临着一个包含200行块的块,前面有3行标题,我想考虑所有这些行。
我已尝试在scilab帮助网站上的示例中为csvRead指定示例的数据范围(示例位于页面底部)(https://help.scilab.org/doc/6.0.0/en_US/csvRead.html),但我总是会出现相同的错误消息:
The line and/or colmun indices are outside of the limits
或
Error in the column structure.
我的前三行是标题,我知道这可能会导致问题,但是即使我在块范围内将其省略,我仍然遇到相同的问题。
否则,我的数据将被排序,以使我拥有三行标题(两行包含仅一两列的标题,一行包含所有列的标题),200行数据和空白行-这代表一幅图像的数据,并且文件中有大约500幅图像,我希望能够读取和处理所有图像并跟踪标题,因为它们指出了以后需要参考的图像编号。示例:
DTN-dist_Devissage-1_0006_0,,,,,,
L0,,,,,,
X [mm],Y [mm],W [mm],exx [1] - Lagrange,eyy [1] - Lagrange,exy [1] - Lagrange,Von Mises Strain [1] - Lagrange
-1.13307,-15.0362,-0.00137507,7.74679e-05,8.30045e-05,5.68249e-05,0.00012711
-1.10417,-14.9504,-0.00193334,7.66086e-05,8.02914e-05,5.43132e-05,0.000122655
-1.07528,-14.8647,-0.00249155,7.57493e-05,7.75786e-05,5.18017e-05,0.0001182
有人对此有解决方案吗?
在修改后的Scilab帮助示例版本之后,我当前的代码如下所示(我尝试过更改blocksize和iblock值以包括/省略标头:
blocksize=200;
C1=1;
C2=14;
iblock=1
while (%t)
R1=(iblock-1)*blocksize+4;
R2=blocksize+R1-1;
irange=[R1 C1 R2 C2];
V=csvRead(filepath+filename,",",".","",[],"",irange);
iblock=iblock+1
end
答案 0 :(得分:0)
您的问题很多是由于csv文件中的逗号数量不一致引起的。在LibreOffice Calc
中将其打开并保存,即使在空行上也可以放入正确数量的逗号。
您当前的代码未将R1放置在值的开头。正确的公式是
R1=(iblock-1)*(blocksize+blanksize+headersize)+1+headersize;
当前,由于R1大于行数,因此您的代码将引发错误和文件结尾。要解决此问题,您可以指定最大块数,或针对行数测试R1的值。
在解决带有大文件的探针时,出现了两个问题:
csvRead
的每次调用确实很慢,因为它每次调用时都会处理整个文件(1秒/块!)我的想法是读取整个文件并将其存储在字符串矩阵中(由于mgetl
从6.0.0开始有所改进),然后在子矩阵上使用csvTextScan
。这样做还消除了手动编写块/行数的情况。
代码如下:
clear all
clc
s = filesep()
filepath='.'+s;
filename='DTN_full.csv';
// header is important as it as the image name
headersize=3;
blocksize=200;
C1=1;
C2=14;
iblock=1
// let save everything. Good for the example.
bigstruct = struct();
// Read all the value in one pass
// then using csvTextScan is much more efficient
text = mgetl(filepath+filename);
nlines = size(text,'r');
while ( %t )
mprintf("Block #%d",iblock);
// Lets read the header
R1=(iblock-1)*(headersize+blocksize+1)+1;
R2=R1 + headersize-1;
// if R1 or R1 is bigger than the number of lines, stop
if sum([R1,R2] > nlines )
mprintf('; End of file\n')
break
end
// We use csvTextScan ony on the lines that matters
// speed the program, since csvRead read thge whole file
// every time it is used.
H=csvTextScan(text(R1:R2),",",".","string");
mprintf("; %s",H(1,1))
R1 = R1 + headersize;
R2 = R1 + blocksize-1;
if sum([R1,R2]> nlines )
mprintf('; End of file\n')
break
end
mprintf("; rows %d to %d\n",R1,R2)
// Lets read the values
V=csvTextScan(text(R1:R2),",",".","double");
iblock=iblock+1
// Let save theses data
bigstruct(H(1,1)) = V;
end
并返回
Block #1; DTN-dist_0005_0; rows 4 to 203
....
Block #178; DTN-dist_0710_0; rows 36112 to 36311
Block #179; End of file
Time elapsed 1.827092s