How to modify text file
조회 수: 14 (최근 30일)
이전 댓글 표시
I'm trying to overwrite data in the following text file:
"L_Chamber"=7.748
"D_Chamber"=6.757
"L_Nozzle"=8.545
"D_Nozzle"=8.509
"D_Throat"=2.941
"Theta_N"=31.278
using the following code:
fileID = fopen('equations.txt');
fprintf(fileID,'"L_Chamber"= %8.3f\n',L_Chamber);
I'm trying to update the numerical value only. I don't get any errors, but the text file doesn't change no matter what I do.
댓글 수: 0
채택된 답변
추가 답변 (1개)
KSSV
2019년 7월 17일
fid = fopen('data.txt') ;
S = textscan(fid,'%s') ;
fclose(fid) ;
S = S{1} ;
% Find the string you want to change
idx = contains(S,'L_Chamber') ;
% Replace
S(idx) = {'"L_Chamber"=8.34'} ;
% Write to text file
fid = fopen('new.txt','w');
fprintf(fid,'%s\n',S{:});
fclose(fid);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!