Change line in a text file
Show older comments
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
Ameer Hamza
on 1 Jun 2018
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?
FM
on 4 Jun 2018
Jan
on 4 Jun 2018
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.
Jan
on 4 Jun 2018
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?
FM
on 4 Jun 2018
Jan
on 4 Jun 2018
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.
FM
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.
FM
on 5 Jun 2018
Answers (1)
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
FM
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
FM
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.
Categories
Find more on Characters and Strings 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!