How to modify text file

조회 수: 14 (최근 30일)
nick12391
nick12391 2019년 7월 17일
답변: nick12391 2019년 8월 3일
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.

채택된 답변

nick12391
nick12391 2019년 8월 3일
I wasn't giving fopen a permission. Should have written:
fileID = fopen('equations.txt','w');
fprintf(fileID,'"L_Chamber"= %8.3f\n',L_Chamber);

추가 답변 (1개)

KSSV
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);

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by