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!