필터 지우기
필터 지우기

ARRANGING TEXT FILE?

조회 수: 3 (최근 30일)
AHMET TATAR
AHMET TATAR 2017년 10월 8일
댓글: Cedric 2017년 10월 8일
Hi,
I am working on a dynamic analysis. I am trying to run ANSYS on MATLAB. Therefore I have to change some value in my text file. Text file is large but i need to change e few line as follows;
loadvari47rotz(2,1,1) = *0.174532925199433* ??CHANGING_LINE-1
loadvari47rotz(3,1,1) = *0.349065850398866* ??CHANGING_LINE-2
I want to change above value for each step. Text file is at attachment and you can find changing lines CTRL+F "??CHANGING_LINE-1" and "??CHANGING_LINE-1".
I will be so happy if you help me.
Best Regards, Ahmet TATAR

채택된 답변

Cedric
Cedric 2017년 10월 8일
편집: Cedric 2017년 10월 8일
Here is an example, and then we can tailor the solution to your needs from there:
content = fileread( 'TEST.txt' ) ;
content = regexprep( content, '(?<=_loadvari47rotz\(2,1,1\) = )\S+', sprintf( '%f', 0.123 )) ;
content = regexprep( content, '(?<=_loadvari47rotz\(3,1,1\) = )\S+', sprintf( '%f', 0.456 )) ;
fId = fopen( 'TEST_updated.txt', 'w' ) ;
fwrite( fId, content ) ;
fclose( fId ) ;
This can easily be updated for matching/replacing other parts, looping through files, etc.
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 10월 8일
To emphasize slightly: Cedric produces a new output file that has the right content, rather than changing the values "inside" the file. Updating a text file "in place" is only possible if the replacement text has exactly the same length as the original text. The update process is enough of a nuisance to do reliably that on the whole producing a new output file is easier and more reliable.

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

추가 답변 (1개)

AHMET TATAR
AHMET TATAR 2017년 10월 8일
Dear Cedric and Walter,
This is very helpful for me. thank you for information.
  댓글 수: 1
Cedric
Cedric 2017년 10월 8일
Our pleasure!
And thank you Walter for the complement :)

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by