Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...