Change line in a text file

Hello,
I have a text file where different reactions are written as well as their kinetics constants para1 para2 para3
For example:
A+ B => C + D para1 para2 para3
This text file is read by another software that solves a set of equations. I need to test out different values for each parameter para1 para2 and para3. These parameters are in the second to last line of the text file. It’s a very big text file and I don’t want matalb to read and then rewrite everything each time. I want to do this in a loop and for every iteration I test out a different value for each parameter (so the values written on the text file are not always the same). Does anyone know how I can do this?
Thanks

10 Comments

Do you want to create several files with different values of para1, para2 and para3? Or do you want to modify the same file several times?
I want to modify the same file several times
Please post a small but relevant example. The less we have to guess for the answer, the more likely is that it matches your needs.
FM
FM on 4 Jun 2018
Edited: FM on 5 Jun 2018
My text file is really big and the last lines are like this
...
A1+ B1 => C1 + D1 para11 para12 para13
A2+ B2 => C2 + D2 para21 para22 para23
A3+ B3 => C3 + D3 para31 para32 para33
END
The text file already exists and it has over 500 lines. I want to write a code that allows me to change the values of para31, para32 and para33.
This text file is read by another software that simulates gas phase chemistry, all I need matlab to do is modify the text file. I need to test different values for para31, para32 and para33
For example if I want to test 4 values for each parameter, I store the values in vectors P1 (for para31), P2(for para32) and P3 (for para33). P1, P2 and P3 would each have 4 values.
I want a code that does this
for i=1:4
Replace “A3+ B3 => C3 + D3 para31 para32 para33” by A3+ B3 => C3 + D3 P1(i) P2(i) P3(i) [Edited]
Launch software
end
Does the text file really contain additional blank lines? It would be enough if you post the last few lines of the file, as long as this clears uniquely, what the contents of the file is.
How can "the last lines" be identified? What is the contents of the variables P1, P2, P3?
The last 3 lines of the text file are
C6H5+C2H2=C6H5C2H2 12E41 -9.26 16E3
C2H2 => 2C + H2 3E6 0 40E3
END
The line I want to change is "C2H2 => 2C + H2 3E6 0 40E3" I want to change the values 3E6 0 40E3, however the reaction itself C2H2 => 2C + H2 doesn't change so that could be a way of identifying the line
The vectors P1 P2 and P3 contain the values I want to test out, for example
P1= (2E6, 4E6, 5E6)
P2=(10, 20,0)
P3=(20E3,30E3 50E3)
The last 3 lines of the text file are
C6H5+C2H2=C6H5C2H2 12E41 -9.26 16E3
C2H2 => 2C + H2 3E6 0 40E3
END
These are 5 lines. Of course I can guess, that the empty lines do not belong to the real text. But why do you insert them at all? Please read Format in the forum to use formatting to improve the readability. Thanks.
The line I want to change is "C2H2 => 2C + H2 3E6 0 40E3"
Formerly you said it is the line:
A2+ B2 => C + D para21 para22 para23
Please, FM, do not assume that we can read your mind. Explain clearly and uniquely, what you want to replace.
I apologize, the line I want to change is
A3+ B3 => C + D para31 para32 para33
Which corresponds to
C2H2 => 2C + H2 3E6 0 40E3
What I want to change is the values 3E6 0 40E3, the beginning of the line "C2H2 => 2C + H2" stays the same so for example after modifying the line once I would have
C2H2 => 2C + H2 2E6 10 20E3
The blank lines are there in the text file, I apologize if I said 3 lines instead of 5. I didn't think having blank lines in between the text would make a difference but I was wrong, I am sorry. I am not assuming you can read my mind, I have trouble explaining exactly what I need to do but I'm trying to do my best.
I use this code that you suggested (in a file named Rewrite)
CStr = strsplit(fileread('Test.txt'), '\n');
P1=[2E6,4E6,5E6];
P2=[10,20,30];
P3=[20E3,30E6,50E6];
index = strncmp(CStr, 'C2H2 => 2C + H2 ', 16);
for i = 1:3
CStr{index} = sprintf('C2H2 => 2C + H2 %d %d %d', P1(i), P2(i), P3(i));
fid = fopen(fullfile(tempdir, 'Test2.txt'));
if fid == -1
error('Cannot open file.');
end
fprintf(fid, '%s\n', CStr{:});
fclose(fid);
end
and this time the complete error message is
Error using Rewrite (line 13)
Cannot open file.
However if I use this code instead, I don't get any errors
CStr = strsplit(fileread('Test.txt'), '\n');
P1=[2E6,4E6,5E6];
P2=[10,20,30];
P3=[20E3,30E6,50E6];
index = strncmp(CStr, 'C2H2 => 2C + H2 ', 16);
for i = 1:3
CStr{index} = sprintf('C2H2 => 2C + H2 %d %d %d', P1(i), P2(i), P3(i));
fid = fopen('Test2.txt', 'w+');
if fid == -1
error('Cannot open file.');
end
fprintf(fid, '%s\n', CStr{:});
fclose(fid);
end
I don't know what
fid = fopen(fullfile(tempdir, 'Test2.txt'));
does exactly so that's why I tried
fid = fopen('Test2.txt', 'w+');
instead.
Anyways thanks
Jan
Jan on 5 Jun 2018
Edited: Jan on 5 Jun 2018
It is still not clear to me, what "correspond" means here:
A3+ B3 => C + D para31 para32 para33
Which corresponds to
C2H2 => 2C + H2 3E6 0 40E3
But I've forgot the 'w' flag in the fopen command. Without it, fopen tries to open the file for reading. When the file does not exist, the fopen fails. With 'w' the file is opened for writing. 'w+' allows reading and writing and is not useful here.
It is better to use the message in case of errors:
[fid, msg] = fopen('Test2.txt', 'w');
if fid == -1
error('Cannot open file: %s', msg);
end
I've fixed my answer and inserted the 'w' specifier.
By the way: Do not get confused by questions for clarifications. The purpose of this forum is to solve Matlab problems. It is the nature of problems, that not all details are clear initially. Even if I'm probing, the goal is to solve your problem.
The text file contains chemical reactions and the data necessary to calculate their kinetic constants. At first to explain my problem I was trying to provide an example without actually giving the names of the components and the values of the parameters. That's why I used A1,A2,A3... and para31, para32,para33... but then when you asked for the actual content of the text file I posted the actual chemical reaction with the values of the kinetic constants
So in my example
A3+ B3 => C3 + D3 para31 para32 para33
A3, B3, C3 and D3 are chemical components. The actual reaction and the data necessary to calculate the kinetic constant is
C2H2 => 2C + H2 3E6 0 40E3
So when I say that "it corresponds" I mean
  • A3 is C2H2
  • there is no B3
  • C is 2C (2 Carbon molecules)
  • D is H2
  • para1=3E6
  • para2=0
  • para3=40E3
I added the message in case of errors and everything works, thank you for your help

Sign in to comment.

Answers (1)

Jan
Jan on 4 Jun 2018
Edited: Jan on 5 Jun 2018
If the file has 500 lines only, importing the complete file and creating a new one is cheap an easy. Note that 500 lines of text are a lot of work for a human, but a cookie for a computer.
CStr = strsplit(fileread(FileName), '\n');
index = strcmp(CStr, 'A2+ B2 => C + D para21 para22 para23'); % [EDITED]
for i = 1:4
CStr{index} = sprintf('A2+ B2 => C + D %d %d %d', P1(i), P2(i), P3(i));
fid = fopen(fullfile(tempdir, 'OutputFile.txt'), 'w'); % [EDITED 2]
if fid == -1
error('Cannot open file.');
end
fprintf(fid, '%s\n', CStr{:});
fclose(fid);
... Launch software
end
Here I guess, that P1 etc is a numerical array. If this does not match, please explain the details.

4 Comments

Hi, thanks Jan,
I tried out what you suggested but when I write the line
index = strcmp(Str, 'A2+ B2 => C + D para21 para22 para23');
There are only 0 in logical variable index, it's as if it didn't find the charachters 'A2+ B2 => C + D para21 para22 para23' and it tells me
Expected one output from a curly brace or dot indexing expression, but there were 0 results.
I tried on another text file just by writing
Hello
Bye
And if I write
index = strcmp(Str, 'Hello');
it still sends me the same Expected one output from a curly brace or dot indexing expression, but there were 0 results.
Jan
Jan on 4 Jun 2018
Edited: Jan on 4 Jun 2018
This was a typo. See [EDITED]: Replace "Str" by "CStr". "Str" seems to be a variable left over from any other operations in you workspace. Prefer to store code in functions, such that the workspace is clean.
You can find the source of such problems using the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
Yes, I had changed Str to CStr because otherwise it was an unknown variable, I still get the problem I explained above though
Jan
Jan on 4 Jun 2018
Edited: Jan on 4 Jun 2018
Now I'm guessing what "explained above" exactly means. Please post the code you are using and a copy of the complete error message.
If you did not meant
'A2+ B2 => C + D para21 para22 para23'
but a line, which starts with
'A2+ B2 => C + D '
use:
index = strncmp(CStr, 'A2+ B2 => C + D ', 16);
or whatever is suitable to identify the line you want to replace.

Sign in to comment.

Categories

Asked:

FM
on 1 Jun 2018

Edited:

FM
on 5 Jun 2018

Community Treasure Hunt

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

Start Hunting!