how to amend an existing text file in a certain line and create new ones
Ältere Kommentare anzeigen
Hello all
I've got a text file and I need to change only one parameter of it many times (in a for loop).
Until now, I've located the line where my parameter is, I've changed it and NOW I NEED TO WRITE THE NEW PARAMETER IN A NEW TEXT FILE, WHOSE ONLY DIFFERENCE WITH THE PREVIOUS ONE WOULD BE THE DIFFERNT PARAMETER VALUES.
My code until now looks like that.
%Define the range of the parameter sathh
%Assume that sathh can vary between 0.01 and 0.05
parameterA_range = 0.01:0.01:0.05;
%The loop counter
for i=1:length(parameterA_range)
%Extract each value from the range of the parameterA_range
param1 = parameterA_range(i)
%Replicate each of these values 4 times (4 soil layers) and create the new
%parameter set
parameterA = repmat(param1,1,4)
%Replace the parameterA with the new parameterA parameters.
%Open the txt file and scan through it
fid = fopen('sensanalysis3.txt');
C = textscan(fid,'%s','delimiter','\n');
%Find the line number 188, where the parameterA is
line188 = C{1}{188}
line188_changed =strrep(line188,'0.0453, 0.0453, 0.0453, 0.0453',num2str(sathh))
So, in line 188 I need to have the new values of parameterA.
Any ideas?
1 Kommentar
Christina
am 23 Sep. 2011
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Data Import and Export finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!