How can i replace several lines in the same .txt file using "strrep" command?

1 Ansicht (letzte 30 Tage)
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 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 1 Aug. 2015
Just to make things clear, give a short example and the expected result.
Roberto Falcone
Roberto Falcone am 1 Aug. 2015
I want to find the following lines (that are not present in sequence so as reported here) in MaterialiprovaRFR.tcl :
uniaxialMaterial Concrete01 1 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 2 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 3 16.36 0.002 3.27 0.005
uniaxialMaterial Concrete01 4 16.36 0.002 3.27 0.005
substitute them with 4 new ones and save the changed lines (together with the unchanged ones) in a copy of the file "MaterialiprovaRFR.tcl", named "Materialiprova.tcl". This last file should have the same strings of its original version with the exception of four new lines (which i cannot report here becousethe last two numbers are not fixed becouse they depend on genetic operator)
But just as an example i wrote :
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
The syntax of "strrep" command is :
modifiedStr = strrep(origStr, oldSubstr, newSubstr)
Is there a way to use the "strrep" command several times
oldSubstr1------> newSubstr1
oldSubstr2------> newSubstr2
oldSubstr3------> newSubstr3
oldSubstr4------> newSubstr4
within the same "origStr" and then print all in a single txt file?
Is it clearer now? Thanks

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by