Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

csv - Matlab xlsread cutting off file after row 1048576

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('
'));
row = [[1; row(1:end-1)'+2] row' - 1];
% Fix-up if there is no 
 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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...