我有一个大的制表符分隔文件(10000 行,15000 列),想将它导入 Matlab。
我尝试通过以下方式使用 textscan 函数导入它:
function [C_text, C_data] = ReadDataFile(filename, header, attributesCount, delimiter,
attributeFormats, attributeFormatCount)
AttributeTypes = SetAttributeTypeMatrix(attributeFormats, attributeFormatCount);
fid = fopen(filename);
if(header == 1)
%read column headers
C_text = textscan(fid, '%s', attributesCount, 'delimiter', delimiter);
C_data = textscan(fid, AttributeTypes{1, 1}, 'headerlines', 1);
else
C_text = '';
C_data = textscan(fid, AttributeTypes{1, 1});
end
fclose(fid);
AttributeTypes{1, 1} 是一个字符串,它描述了每列的变量类型(在这种情况下,有 14740 个浮点类型变量和 260 个字符串类型变量,因此 AttributeTypes{1, 1} 的值是 '%f%f..... .%f%s%s...%s 其中 %f 重复 14740 次和 %s 260 次)。
当我尝试执行
>> [header, data] = ReadDataFile('data/orange_large_train.data.chunk1', 1, 15000, '\t', types, size);
标头数组似乎是正确的(列名已正确读取)。
data是一个 1 x 15000 数组(只导入了第一行而不是 10000)并且不知道是什么导致了这种行为。
我想问题是在这一行引起的:
C_data = textscan(fid, AttributeTypes{1, 1});
但不知道可能出了什么问题,因为帮助参考中有一个类似的示例。
如果你们中的任何人建议任何解决该问题的方法,我将非常感激 - 如何读取所有 10000 行。