How can i replace several lines in the same .txt file using "strrep" command?
1 view (last 30 days)
Show older comments
I'm trying to read a txt file (MaterialiprovaRFR.tcl), create a copy of it but replacing 4 strings in the modified copy ((Materialiprova.tcl) using the command "strrep". Once excecuted the code, the result is not what i expect. In fact, opening the generated new file "Materialiprova.tcl" i read the same line of the original .txt file four times, i.e :
-----Core concrete (confined)
-----Core concrete (confined)
-----Core concrete (confined)
-----Core concrete (confined)
uniaxialMaterial Concrete01 1 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 1 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 1 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 1 16.36 0.002 3.27 0.005
...................................................
The following is part of my code:
fid = fopen('C:\Users\Ro\Desktop\Folder\MaterialiprovaRFR.tcl','r'); %read the original-reference file
fid2 = fopen('C:\Users\Ro\Desktop\Folder\Materialiprova.tcl','wt+'); %create a copy of the previous file in which operate some (4) substitutions of lines.....................................................................
.............................................................................................................
.........................................................
................................................
while ~feof(fid)
a= fgetl(fid);
......................
.....................
oldrows=[stringapil1RFR,stringapil2RFR,stringapil3RFR,stringapil4RFR];
newrows=[strpil1,strpil2,strpil3,strpil4];
for i=1:nvar
substitution=strrep(a, oldrows(i), newrows(i));
fwrite(fid2,[substitution double(sprintf('\n'))]);
end
end
fclose(fid);
fclose(fid2);
i would expect to read in the created Materialiprova.tcl file, a copy of all the strings of the original file with the exception of :
newrows (for example, becouse the last two numbers appearing in each string are calculate by a random procedure)=
uniaxialMaterial Concrete01 1 16 0.002 23.4133 0.0076142
uniaxialMaterial Concrete01 2 16 0.002 20.6701 0.0064092
uniaxialMaterial Concrete01 3 16 0.002 23.4133 0.0076142
uniaxialMaterial Concrete01 4 16 0.002 25.7142 0.0085388
in place of the following lines contained in MaterialiprovaRFR.tcl
oldrows =
uniaxialMaterial Concrete01 1 16 0.002 25.7142 0.0085388
uniaxialMaterial Concrete01 2 16 0.002 25.7142 0.0085388
uniaxialMaterial Concrete01 3 16 0.002 25.7142 0.0085388
uniaxialMaterial Concrete01 4 16 0.002 25.7142 0.0085388
I tryed to employ the "cellfun" command before of recalling "strrep" but Matlab show me a worning. can someone help me? Thanks
2 Comments
Azzi Abdelmalek
on 1 Aug 2015
Just to make things clear, give a short example and the expected result.
Answers (0)
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!