How to overwrite the existing data inside the same file.
조회 수: 7 (최근 30일)
이전 댓글 표시
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
답변 (2개)
TAB
2012년 6월 25일
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
Walter Roberson
2012년 6월 25일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!