How do i replace a string in text file in loop?

I need to replace a text in a notepad in loop. In the below attached file(input), the term 'zout' needs to be replace from 'zout = 0, 0.25' to 'zout = 0.25, 0.50'(and execute a bunch of other commands inside the loop) until 'zout= 28, 30'.I have attached the code(SBDRUN1) used for running the input file below.
Thanks in advance

댓글 수: 3

You can create a new string with sprintf:
zout = sprintf('zout = %d, %d\n',var1, var2);
If this line is inside the a for loop then it will update the string with new variables each iterations. This can then be printed into the file with fprintf or a similar command.
will this replace the zout in the existing file. I need to have zout = 5,10 run one iteration and then replace it as zout=10,12 and run the loop again.
Rik
Rik 2018년 2월 9일
Unless you are using ActiveX, you can't replace text inside a file. What you can do is read the file, make the changes you want, and then writing the result to the text file, replacing it in its entirety.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2018년 2월 8일

댓글:

Rik
2018년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by