Editing a txt file and replace a specifics lines

1 次查看(过去 30 天)
Dear all, my question is about the edition of an txt file and the replacement of the 4 and the 5 line and keep the above ones and the after ones: txtfile = ZZZ.txt
Not change
Not change
Not change
TO change
TO change
Any suggestions please.
  2 个评论
judy abbott
judy abbott 2016-4-8
i look to replace the wholes lines (the 4 and the 5) by other lines, i think to do an counter (while .. do)

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-4-8
编辑:Azzi Abdelmalek 2016-4-8
Read your text file
fid=fopen('ZZZ.txt')
s=textscan(fid,'%s')
fclose(fid)
out=[s{:}]
Then replace whatever you want
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2016-4-8
%lines that replace the two lines of out
line1='abc'
line2='efg'
out(4:5)={line1;line2}
fid=fopen('fic1.txt','w')
for k=1:numel(out)
fprintf(fid,'%s\n',out{k})
end
fclose(fid)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by