简体   繁体   English

使用While循环将大文本文件读入MATLAB

[英]Read Large Text File with a While Loop into MATLAB

I am trying to read a large 3 GB text file into MATLAB, organized by a header with names, and with a delimiter of a space (see below fruit.txt) However, the only data needed is the Grapes Column. 我正在尝试将一个大的3 GB文本文件读入MATLAB,由带有名称的标题组织,并带有空格的分隔符(参见下面的fruit.txt)但是,唯一需要的数据是Grapes Column。 Since it is a huge file, I am using a loop below to only read in one column into Matlab. 由于它是一个巨大的文件,我使用下面的循环只在一列中读入Matlab。 How can I read in only one column of data with the loop below? 如何通过下面的循环只读取一列数据? I have to use a loop and preselection of needed columns since the file is over 3 GB of data. 我必须使用循环和预选所需的列,因为文件超过3 GB的数据。

fruit.txt fruit.txt

Apples Grapes Oranges
3 4 A
4 G 1
6 A 3 
3 4 1
A 6 1
2 2 4

filename = 'fruit.txt'
delimiter = ' ';
formatSpec = '%s%s%s[^\n\r]';

fileID  = fopen(filename, 'r' ) ;
out = {};
k = 0 ;

while ~feof(fileID)
  k = k+1;
  C = textscan(fileID, formatSpec, 'Delimiter', delimiter);

  out{end+1} = Grapes{:,2}; 
end

使用readmatrix并指定一个标题行,并且只需要第2列:

readmatrix(filename, 'FileType','text', 'Delimiter', delimiter, 'NumHeaderLines', 1, 'Range', 'B:B'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM