将某些文本放入matlab中的向量中

Glo*_*ria 5 matlab character

可能重复:
在matlab中从文本文件中查找特定数据

我已经使用以下代码打开了标题为'gos.txt'的文本文件:

s={}; 
fid = fopen('gos.txt'); 
tline = fgetl(fid); 
while ischar(tline) 
   s=[s;tline]; 
   tline = fgetl(fid); 
end
Run Code Online (Sandbox Code Playgroud)

我得到的结果如下:s =

'[Term]'    
'id: GO:0008150'
'name: biological_process'
'namespace: biological_process'
'alt_id: GO:0000004'
'alt_id: GO:0007582'
[1x243 char]
[1x445 char]
'subset: goslim_aspergillus'
'subset: goslim_candida'
'subset: goslim_yeast'
'subset: gosubset_prok'
'synonym: "biological process" EXACT []'
'synonym: "biological process unknown" NARROW []'
'synonym: "physiological process" EXACT []'
'xref: Wikipedia:Biological_process'
'[Term]'    
'id: GO:0016740'
'name: transferase activity'
'namespace: molecular_function'
[1x326 char]
'subset: goslim_aspergillus'
'subset: goslim_candida'
'subset: goslim_metagenomics'
'subset: goslim_pir'
'subset: goslim_plant'
'subset: gosubset_prok'
'xref: EC:2'
'xref: Reactome:REACT_25050 "Molybdenum ion transfer onto molybdopterin, Homo sapiens"'
'//is_a: GO:0003674 ! molecular_function'
'is_a: GO:0008150 ! molecular_function (added by Zaid, To be Removed Later)'
'//relationship: part_of GO:0008150 ! biological_process'
'[Term]'    
'id: GO:0016787'
'name: hydrolase activity'
'namespace: molecular_function'
[1x186 char]
'subset: goslim_aspergillus'
'subset: goslim_candida'
'subset: goslim_metagenomics'
'subset: goslim_plant'
'subset: gosubset_prok'
'xref: EC:3'
'//is_a: GO:0003674 ! molecular_function'
'is_a: GO:0016740 ! molecular_function (added by Zaid, to be removed later)'
'relationship: part_of GO:0008150 ! biological_process'
'[Term]'    
'id: GO:0006810'
'name: transport'
'namespace: biological_process'
'alt_id: GO:0015457'
'alt_id: GO:0015460'
[1x255 char]
'subset: goslim_aspergillus'
'subset: goslim_candida'
'synonym: "small molecule transport" NARROW []'
'synonym: "solute:solute exchange" NARROW []'
'synonym: "transport accessory protein activity" RELATED [GOC:mah]'
'is_a: GO:0016787 ! biological_process'
'relationship: part_of GO:0008150 ! biological_process'
.
.
.
.    
Run Code Online (Sandbox Code Playgroud)

后面的步骤是如何采取某个特征并将其放在一个向量中...例如:我想把所有行包含'id:GO:*******'并将它们放在一个向量中,也是我我想把'is_a:GO:*******'变成一个向量,请注意我不想在同一行之后的字符.

ang*_*nor 6

你可以regexp在这里轻松使用- 它适用于细胞:

matching_lines = s{~cellfun('isempty', regexp(s, '^id: GO'))}

ans =

 id: GO:0008150

ans =

 id: GO:0016740
Run Code Online (Sandbox Code Playgroud)

提取所有以id: GO.开头的行.的cellfun通话单独为您提供了0/1的矢量,其中1表示在一个字符串s您的查询相匹配.

类似的行找到包含的is_a: GO:.从字符串中删除不必要的字符也可以使用regexp.

可以使用以下'tokens'参数完成字符串的部分提取regexp:

tok = regexp(s, '^id: (GO.*)', 'tokens');
idx = ~cellfun('isempty', tok);
v   = cellfun(@(x)x{1}, {tok{idx}});
sprintf('%s ', v{:})

ans =

 GO:0008150 GO:0016740 
Run Code Online (Sandbox Code Playgroud)