How can I change multiple lines in a text file?

3 ビュー (過去 30 日間)
Colin
Colin 2013 年 10 月 17 日
コメント済み: Cedric 2013 年 10 月 20 日
I have pasted a portion of a text file which I would like to edit, an example of what the modified file should look like and also my current output. For each material in the a text file I need to change a value, 2.5e8, to a random variable and I would like to generate many verions of the file with different random variables. The code I am using is as below.
clear all
clc
for n=1:1:20
fin = fopen('Ap1_Tr_Disp_Int.inp')
fout = fopen([num2str(n), '.inp'], 'w')
while ~feof(fin)
s = fgetl(fin)
r = 2.5e8 + 2.5e7*randn(1,1)
s = strrep(s, ' 2.5e8, 0', [' ', num2str(r), ', 0'])
s = strrep(s, ' 2.5e8, 0.005', [' ', num2str(r), ', 0.005'])
fprintf(fout,'%s\n',s)
end
end
fclose(fin)
fclose(fout)
The problem with the code is that it changes both instances of 2.5e8 (see below) to different random variables. I have spent ages trying to figure this out, however I am new to Matlab. Any help would be greatly appreciated!
Original Text File
*Material, name=Steel_1
*Elastic
2e+11, 0.3
*Plastic
2.5e8, 0 (I want to change 2.5e8 to a random variable)
2.5e8, 0.005 (for each material, new variables should be the same)
1e3, 0.0050001
**
*Material, name=Steel_2
*Elastic
2e+11, 0.3
*Plastic
2.5e8, 0
2.5e8, 0.005
1e3, 0.0050001
.....etc
Example of desired output
*Material, name=Steel_1
*Elastic
2e+11, 0.3
*Plastic
2.7e8, 0
2.7e8, 0.005
1e3, 0.0050001
**
*Material, name=Steel_2
*Elastic
2e+11, 0.3
*Plastic
2.35e8, 0
2.35e8, 0.005
1e3 , 0.0050001
.....etc
Example of my current output
*Material, name=Steel_1
*Elastic
2e+11, 0.3
*Plastic
2.3e8, 0
2.9e8, 0.005 (2.3e8 and 2.9e8 should be the same)
1e3, 0.0050001
**
*Material, name=Steel_2
*Elastic
2e+11, 0.3
*Plastic
2.7e8, 0
2.4e8, 0.005 (2.7e8 and 2.4e8 should be the same)
1e3 , 0.0050001
.....etc

採用された回答

Nishitha Ayyalapu
Nishitha Ayyalapu 2013 年 10 月 17 日
編集済み: Nishitha Ayyalapu 2013 年 10 月 17 日
Here is the working code. Small tweaks to your existing code solved the problem.
1.) Generate a random only for new type of material.
2.) Do the String replace only once whenever the line has target string "2.5e08".
doc strfind
Below is the Modified Code:
clear all
clc
for n=1:1:20
fin = fopen('Ap1_Tr_Disp_Int.inp') ;
fout = fopen([num2str(n), '.inp'], 'w');
occur = 1; %counting ooccurrences of the string '2.5e8'
while ~feof(fin)
%generates new random number only for odd num of occurrences
if (mod(occur,2))
r = 2.5e8 + 2.5e7*randn(1,1);
end
s = fgets(fin);
if (~isempty(strfind(s,' 2.5e8')))
reps = num2str(r(1));
s = strrep(s, ' 2.5e8', [' ',reps])
occur = occur + 1; %if the target string occurs increment
end
fprintf(fout,'%s',s)
end
end
fclose(fin);
fclose(fout);
Hope this helps !!
  1 件のコメント
Colin
Colin 2013 年 10 月 17 日
Brilliant. Thanks a lot!

サインインしてコメントする。

その他の回答 (1 件)

Cedric
Cedric 2013 年 10 月 18 日
編集済み: Cedric 2013 年 10 月 18 日
Just for fun, here is a one-liner for reading/replacing (that I write on 3 lines for sake of clarity):
repl = regexprep( fileread( 'Ap1_Tr_Disp_Int.inp' ), ...
'(Plastic\s+)2.5e8(.+?)2.5e8', ...
'${sprintf([$1,''%.1e'',$2,''%.1e''],2.5e8+2.5e7*repmat(randn(1),1,2))}' ) ;
As you are always dealing with the same source file, the whole code would reduce to something like ..
content = fileread( 'Ap1_Tr_Disp_Int.inp' ) ;
newContent = @() regexprep( content, ...
'(Plastic\s+)2.5e8(.+?)2.5e8', ...
'${sprintf([$1,''%.1e'',$2,''%.1e''],2.5e8+2.5e7*repmat(randn(1),1,2))}' ) ;
for k = 1 : 20
fid = fopen( sprintf('%02d.inp', k), 'w' ) ;
fwrite( fid, newContent() ) ;
fclose( fid ) ;
end
  2 件のコメント
Colin
Colin 2013 年 10 月 20 日
編集済み: Colin 2013 年 10 月 20 日
Thanks for the suggestion. It appears to be much more efficient that any other solutions I have tried.
Cedric
Cedric 2013 年 10 月 20 日
You're welcome. The drawback is that it involves a call to REGEXPPREP that nobody will ever want to debug if there is a problem ;-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by