Matlab xlsread cutting off file after row 1048576

前端 未结 1 1083
灰色年华
灰色年华 2020-12-12 03:34

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 ha

相关标签:
1条回答
  • 2020-12-12 04:23

    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');
    
    0 讨论(0)
提交回复
热议问题