首页 文章

Matlab xlsread在行1048576之后切断文件

提问于
浏览
1

除了xlsread(file.csv)之外,还有其他方法可以将Excel格式的.csv导入Matlab吗?

我的文件包含2830082行,xlsread读取时似乎有1048576行的限制 - 其余的被截断 .

该文件看起来像:

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

因此使用csvread(..)因日期格式不起作用 .

1 回答

  • 2

    我发现将BIG csv文件读入Matlab的最快方法是对它们进行内存映射并将内容解析为单个字符串 . 尝试使用此示例代码:

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

相关问题