2010-08-05 151 views
0

我有一个大的制表符分隔的文件(10000行,15000列),并想将其导入到Matlab。如何使用textscan读取文件?

我已经尝试使用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); 

阵列似乎是正确的(列名已经被正确读取)。

数据是一个1 x 15000数组(仅导入第一行而不是10000)并且不知道是什么导致了这种行为。

我想这个问题是在这一行造成的:

C_data = textscan(fid, AttributeTypes{1, 1}); 

,但不知道什么可能是错误的,因为在帮助说明一个类似的例子。

如果你们中的任何人提出了解决问题的方法,我将非常感激 - 如何读取所有10000行。

回答

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});