How to modify few numbers in a random text file?

조회 수: 2 (최근 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 CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by