Matlab xlsread在行1048576之后切断文件

时间:2012-05-18 15:18:12

标签: matlab csv import xls

除了xlsread(file.csv)之外,还有其他方法可以将Excel格式的.csv导入Matlab;

我所拥有的文件包含2830082行,xlsread在阅读时似乎有1048576行的限制 - 其余的被截断。

该文件如下:

Time, Value
12:07:29, -1.13
12:07:29, -7.54
...

因为日期格式,使用csvread(..)是行不通的。

1 个答案:

答案 0 :(得分:2)

我发现将BIG csv文件读入Matlab的最快方法是对它们进行内存映射并将内容解析为单个字符串。尝试使用此示例代码:

fname = 'file.csv';
fstats = dir(fname);
% Map the file as one long character string
m = memmapfile(fname, 'Format', {'uint8' [ 1 fstats.bytes] 'asUint8'});
textdata = char(m.Data(1).asUint8);

% Find the end of each line, and use the line ends to form an index array
row = strfind(textdata, sprintf('\r\n'));
row = [[1; row(1:end-1)'+2] row' - 1];
% Fix-up if there is no \r\n at the end of the last line
if (row(end) < fstats.bytes - 2)
    row = [row; [row(end) + 2, fstats.bytes]];
end
numrows = size(row, 1);
% Create output variables
Time = zeros(numrows, 1);
Value = zeros(numrows, 1);

% Parse each line of the data (I'm ignoring the first line for simplicity)
for RowNum = 2:numrows
    data = textscan(textdata(row(RowNum,1):row(RowNum,2)), '%[^,]%f', 'delimiter', ',');
    Time(RowNum) = datenum(data{:});
    Value(RowNum) = data{2};
end

% Remove the file mapping
clear('m');