Editing a txt file and replace a specifics lines

7 Ansichten (letzte 30 Tage)
judy abbott
judy abbott am 8 Apr. 2016
Kommentiert: judy abbott am 8 Apr. 2016
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 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 8 Apr. 2016
How you replace these lines?
judy abbott
judy abbott am 8 Apr. 2016
i look to replace the wholes lines (the 4 and the 5) by other lines, i think to do an counter (while .. do)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 8 Apr. 2016
Bearbeitet: Azzi Abdelmalek am 8 Apr. 2016
Read your text file
fid=fopen('ZZZ.txt')
s=textscan(fid,'%s')
fclose(fid)
out=[s{:}]
Then replace whatever you want
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 8 Apr. 2016
%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)
judy abbott
judy abbott am 8 Apr. 2016
Ok it's done

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by