Matlab xlsread 在行 1048576 后切断文件 [英] Matlab xlsread cutting off file after row 1048576

查看:27
本文介绍了Matlab xlsread 在行 1048576 后切断文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any other way of importing an Excel formatted .csv into Matlab other than xlsread(file.csv);

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

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.

文件看起来像:

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

因此,由于日期格式的原因,使用 csvread(..) 是行不通的.

So using csvread(..) isn't going to work because of the date format.

推荐答案

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

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

这篇关于Matlab xlsread 在行 1048576 后切断文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆