% getting the text and the first word
text_in_file = fileread('oldfle.txt');
first_word = regexp(text_in_file, '\S*', 'match','once');
% generating the new string
str = regexprep(text_in_file,'[\n\r]+',['\n\n' first_word ' ']);
% writing to the file
fid = fopen('newfile.txt', 'wt');fprintf(fid, str);fclose(fid);
这里是一个修改后的代码,将处理与2个基因的许多行的情况。它重置计数并开始在单基因行前插入新的基因名称。那是你想要的吗?
% getting the text
text_in_file = fileread('oldfile.txt');
% splitting into rows
rows = regexp(text_in_file,'\n','split');
% number of genes in the rows
A = cellfun(@(x) numel(regexp(x, '\t')), rows);
% row indices with two genes
two_word_rows = find(A==2);
% first genes
first_words = cellfun(@(x) regexp(x, '\S+', 'match', 'once'), rows(two_word_rows), 'UniformOutput' , false);
% modifying the rows
for i=setdiff(1:numel(rows), two_word_rows) % exclude the two gene rows
last_idx = find(two_word_rows<i,1,'last'); % which word to add?
rows{i} = sprintf('%s\t%s', char(first_words(last_idx)), rows{i});
end
% writing to the file
fid = fopen('newfile.txt', 'wt');
fprintf(fid, '%s', rows{:});
fclose(fid);
请不要仅仅复制和粘贴代码。尝试通过它,阅读注释并查看使用函数的文档。
请更明确的关于你想实现当你有2个基因多行..什么你想保持第一行的基因在眼前每一行还是你想继续重复的新基因?看到我对这个问题所做的修改,我理解正确吗? –
谷歌驱动器中的文件只有256 kb,不能调用这个大文件。它只是整个文件的一部分吗? – Gelliant