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.

采纳的回答

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