How to modify few numbers in a random text file?

1 view (last 30 days)
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.

Accepted Answer

per isakson
per isakson on 5 Feb 2021
Edited: per isakson on 5 Feb 2021
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

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!