matlab:从没有特定格式

时间:2018-02-27 12:24:57

标签: matlab text-files

如何读取包含2行标题的文本文件,然后读取我要提取的1到3或4行数字并将数字保存到数组。

  

#Sequence Data:seqnum = 1; seqlen = 700;

     

#Model Data:run_type = Metagenomic

     

1_1_636 _ +

     

#Sequence Data:seqnum = 4; seqlen = 700; seqhdr =" frag4",

     

#Model Data:run_type = Metagenomic; model =" 24 |

     

1_3_302 _ +

     
    

2_345_698 _ +

  

1 个答案:

答案 0 :(得分:0)

一种方法是简单地逐行扫描文本文件fgetl

fid = fopen('file.txt');
while ~feof(fid)
    tline = fgetl(fid);
    disp(tline)
end % for
fclose(fid);

参数tline则是一个可以逐个转移到数字的字符串,例如:

numbers = [];
fid = fopen('file.txt');
fgetl(fid);
fgetl(fid);
tline = fgetl(fid);
tline = strsplit(tline,'_');
numbers = [numbers ; str2double(tline{1}) str2double(tline{2}) str2double(tline{3})];
fgetl(fid);
fgetl(fid);
tline = fgetl(fid);
tline = strsplit(tline,'_');
numbers = [numbers ; str2double(tline{1}) str2double(tline{2}) str2double(tline{3})];
tline = fgetl(fid);
tline = strsplit(tline,'_');
numbers = [numbers ; str2double(tline{1}) str2double(tline{2}) str2double(tline{3})];
fclose(fid);

输出:

numbers =
     1     1   636
     1     3   302
     2   345   698

更通用的方法,如果所有注释行都以#:

开头
numbers = [];
fid = fopen('file.txt','r');
while ~feof(fid)
    tline = fgetl(fid);
    if ~strcmp(tline(1),'#')
        tline = strsplit(tline,'_');
        numbers = [numbers ; str2double(tline{1}) str2double(tline{2}) str2double(tline{3})];
    end % if
end % for
fclose(fid);