how to amend an existing text file in a certain line and create new ones

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

Correction: in the last command:
line188_changed =strrep(line188,'0.0453, 0.0453, 0.0453, 0.0453',num2str(parameterA))

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 23 Sep. 2011
When we manually edit a text file, we can go to a particular line, select the whole line, over-write it with some new text and then save the file.
When using fopen() and fprintf(), we can not really do this over-writing selectively, especially when the number of characters in that line is varying. If the text you are overwriting is always at the same position and the length is the same, you can use fseek() to find the position and then over-write the fixed-length of text.
In your case, the only option is to read the whole file, change the content as needed and then write to a different file. After it is done, you can use delete() and movefile() to delete the old file, rename the new file to the old file name.

4 Kommentare

Thank you very much for your reply.
I have already found the certain line that I want to change and I do have the new parameter values ready (in a character format).
I just don't know how to overwrite that string to my old text file to the specific position.
I don't want to change anything else, except of these parameter values.
You've got line188_changed, so
C{1}{188}=line188_changed;
fid_out=fopen('output.txt','wt');
fprintf(fid_out,'%s\n',C{1}{:});
fclose(fid_out);
You'll get the output.txt file with the changed parameter. The rest, you just need to delete the old text file and rename output.txt to that file name.
Thanks a lot!
It worked!
The only difference is that I have to type
fprintf(fid_out,'%s\n',C{1}{1:564})
in order to get the whole of the amended text file, not only one line.
Great! FYI, C{1}{:} should be the same as C{1}{1:564}

Melden Sie sich an, um zu kommentieren.

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!

Translated by