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');
1 回答
我发现将BIG csv文件读入Matlab的最快方法是对它们进行内存映射并将内容解析为单个字符串 . 尝试使用此示例代码: