Matlab xlsread cutting off file after row 1048576

2020-02-07 07:10发布

Is there any other way of importing an Excel formatted .csv into Matlab other than xlsread(file.csv);

The file I have contains 2830082 lines, and xlsread seems to have a limit of 1048576 lines when reading it - the rest gets cut off.

The file looks like:

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

So using csvread(..) isn't going to work because of the date format.

1条回答
【Aperson】
2楼-- · 2020-02-07 07:48

I've found the fastest way to read BIG csv files into Matlab is to memory-map them and parse the contents as a single string. Try playing with this example code:

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');
查看更多
登录 后发表回答