How to modify few numbers in a random text file?

1 回表示 (過去 30 日間)
Jay Vaidya
Jay Vaidya 2021 年 2 月 4 日
編集済み: per isakson 2021 年 2 月 5 日
I am uploading a text file related to a model in an electronic simulator. How to modify the 4 parameters named with
toxe = %1.1d
toxp = %1.1d
toxm = %1.1d
vth0 = %1.1d
If this text file didn't have format specifier, I would just write this file using
model = fileread(fullfile(pathname_model,filename_model));
File_model = fopen(fullfile(dirname,filename_model),'w');
fprintf(File_model,'%s',model);
fclose(File_model);
Since now I want to modify a few parameters each time I run my code, I edited the text file and put format specifier but I don't know how can I print the modified file now.
Is this correct?
model = fileread(fullfile(pathname_model,filename_model));
File_model = fopen(fullfile(dirname,filename_model),'w');
fprintf(File_model,'%s',model, par1, par2, par3, par4);
fclose(File_model);
where par1 to par4 are the first 4 parameter values that I listed at the top.

採用された回答

per isakson
per isakson 2021 年 2 月 5 日
編集済み: per isakson 2021 年 2 月 5 日
Try this
%%
chr = fileread('model_v2.txt');
ffs = 'model_out.txt';
fid = fopen( ffs, 'w' );
[~] = fprintf( fid, chr, 1.2, 3.4, 5.6, 7.8 );
[~] = fclose( fid );
I've changed the format specs in the file, model_v2.txt, to %3.1f because I assume that's what you intended.
Line 8 of model_out.txt now looks like
+tnom = 27 toxe = 1.2 toxp = 3.4 toxm = 5.6

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by