我有 .txt 格式的非常大的数据文件(通常为 30Gb 到 60Gb)。我想找到一种方法来自动删除文件,而无需先将它们导入内存。 我的 .txt 文件由两列数据组成,这是一个示例文件: https://www.dropbox.com/s/87s7qug8aaipj31/RTL5_57.txt
到目前为止,我所做的是将数据导入到变量“C”,然后对数据进行下采样。这种方法的问题是,在程序进行抽取之前,变量“C”常常会填满 MATLAB 的内存容量:
function [] = textscan_EPS(N,D,fileEPS )
%fileEPS: .txt address
%N: number of lines to read
%D: Decimation factor
fid = fopen(fileEPS);
format = '%f\t%f';
C = textscan(fid, format, N, 'CollectOutput', true);% this variable exceeds memory capacity
d = downsample(C{1},D);
plot(d);
fclose(fid);
end
如何修改这一行:
C = textscan(fid, format, N, 'CollectOutput', true);
因此,它可以通过将 .txt 文件的每隔一行或第三行等从磁盘导入到内存中的变量“C”来有效地减少此实例中的数据。
任何帮助将不胜感激。
干杯, 吉姆
PS 我一直在尝试的另一种方法是使用“fread”,但它遇到了同样的问题:
function [d] = fread_EPS(N,D,fileEPS)
%N: number of lines to read
%D: decimation factor
%fileEPS: location of .txt fiel
%read in the data as characters
fid = fopen(fileEPS);
c = fread(fid,N*19,'*char');% EWach line of .txt has 19 characters
%Parse and read the data into floading point numbers
f=sscanf(c,'%f');
%Reshape the data into a two column format
format long
d=decimate((flipud(rot90(reshape(f,2,[])))),D); %reshape for 2 colum format, rotate 90, flip veritically,decimation factor
最佳答案
我相信 textscan 是可行的方法,但是您可能需要采取中间步骤。假设您一次可以轻松阅读 N
行,我会这样做:
- 使用
textscan(fileID,formatSpec,N) 读取 N 行
- 从这些行中采样,存储结果(文件或变量)并删除其余部分
- 只要还有剩余行,就继续执行第 1 步
- 可选,具体取决于您的存储方法:将所有存储的结果合并为一个大样本
应该可以每次只读取 1 行,并决定是否要保留/丢弃它。虽然这应该消耗最少的内存,但我会尝试每次执行几千次以获得合理的性能。
关于matlab - 在 MATLAB 中从磁盘中抽取大型数据文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998719/