我编写了代码来绘制来自非常大的 .txt 文件(20Gb 到 60Gb)的数据。.txt 文件包含两列数据,它们代表我所做的实验中两个传感器的输出。数据文件如此之大的原因是数据以 4M 样本/秒的速度记录。该代码适用于绘制相对较小的 .txt 文件 (10Gb),但是当我尝试绘制较大的数据文件 (60Gb) 时,我收到以下错误消息:
Attempted to access TIME(0); index must be a
positive integer or logical.
Error in textscan_loop (line 17)
TIME =
((TIME(end)+sample_rate):sample_rate:(sample_rate*(size(d,1)))+(TIME(end)));%shift
Time along
我的代码背后的基本思想是通过从磁盘上的 .txt 读取 N 行数据到 RAM 中的 Matlab 变量 C 来节省 RAM,绘制 C 然后清除 C。此过程循环发生,因此数据以块的形式绘制,直到结束.txt 文件已到达。代码可以在下面找到:
Nlines = 1e6; % set numbe of lines to sample per cycle
sample_rate = (1); %sample rate
DECE= 1000;% decimation factor
TIME = (0:sample_rate:sample_rate*((Nlines)-1));%first inctance of time vector
format = '%f\t%f';
fid = fopen('H:\PhD backup\Data/ONK_PP260_G_text.txt');
while(~feof(fid))
C = textscan(fid, format, Nlines, 'CollectOutput', true);
d = C{1}; % immediately clear C at this point you need the memory!
clearvars C ;
TIME = ((TIME(end)+sample_rate):sample_rate:(sample_rate*(size(d,1)))+(TIME(end)));%shift Time along
plot((TIME(1:DECE:end)),(d(1:DECE:end,:)))%plot and decimate
hold on;
clearvars d;
end
fclose(fid);
我认为 while 循环在代码停止执行并显示错误消息之前执行了大约 110 个周期,我知道这一点,因为该图显示了大约 110e7 个数据点,并且循环一次处理 1e6 个数据点。
如果有人知道为什么会发生此错误,请告诉我。
干杯,吉姆