How to overwrite the existing data inside the same file.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have file consisting of many lines. I would like to search and change some of the lines without writing it to a new file . For example,my data goes like this and I would like to increase IDLEPITCH one by one in each iteration and I would like to replace 90 with 91-92-93 .. inside the same file. Is it possible to do this ? otherwise I need to create a new file each time and it will be many files in the end. STALLH Y IDLEPITCH 90 WDIR 0 DFLAP 0.007 DEDGE 0.008
0 Kommentare
Antworten (2)
TAB
am 25 Jun. 2012
f=fopen('YourFile.txt','r');
FData = textscan(f,'%s');
fclose(f);
idx=find(strcmp(FData{1},'IDLEPITCH')==1);
FData{1}{idx+1}='91';
f=fopen('YourFile.txt','w');
for x=1:length(FData{1})
fprintf(f,'%s ',FData{1}{x});
end
fclose(f);
0 Kommentare
Walter Roberson
am 25 Jun. 2012
Unless you will always be replacing text with exactly the same number of characters (e.g., the 90 will never go as high as 100 in the file), then replacing text is tricky. The best way to do it properly is not to do it -- that is, to write out a new file instead. You can delete the old file and rename the new file to the old one after you make the change so that you do not have files accumulating.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time 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!