How to modify text file

17 visualizzazioni (ultimi 30 giorni)
nick12391
nick12391 il 17 Lug 2019
Risposto: nick12391 il 3 Ago 2019
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.

Risposta accettata

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

Più risposte (1)

KSSV
KSSV il 17 Lug 2019
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);

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by