0

我有一个大的制表符分隔文件(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 行。

4

1 回答 1

2

我相信你所有的数据都在那里。如果你往里看data,那里的每个单元格都应该包含整列(10000x1)。您可以将第 i 个单元格提取为带有data{i}.

您可能希望将双精度数据和字符串数据分开。我不知道是什么attributeFormats,你可能可以使用这个数组。但您也可以使用AttributeTypes{1, 1}.

isdouble = strfind(AttributeTypes{1, 1}(2:2:end),'f');
data_double = cell2mat(data(isdouble));

要将字符串数据组合到一个字符串元胞数组中,您可以执行以下操作:

isstring = strfind(AttributeTypes{1, 1}(2:2:end),'s');
data_string = horzcat(data{isstring});
于 2010-08-05T15:13:44.900 回答