i have to write parameters into a .txt file where there are already some written lines (a sort of template) like this
from:
Dictionary kinetics
{
@Kinetics
@Thermodynamics
@Output
}
to:
Dictionary kinetics
{
@Kinetics Cartel1;
@Thermodynamics Cartel2;
@Output Cartel3;
}
the number of whitespaces in beetween isnt a problem, I can' t find a way to do it

 Accepted Answer

Try this
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Open the file for reading in text mode.
outputFileID = fopen(outputFullFileName, 'wt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if contains(textLine, 'Kinetics')
textLineOut = sprintf('%s %f', textLine, Cartel(1))
elseif contains(textLine, 'Thermodynamics')
textLineOut = sprintf('%s %f', textLine, Cartel(2))
elseif contains(textLine, 'Output')
textLineOut = sprintf('%s %f', textLine, Cartel(3))
end
% Output line of text to output file
fprintf(outputFileID, '%s\n', textLineOut);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the files.
fclose(fileID);
fclose(outputFileID);
Untested, so adapt as needed.

6 Comments

I think that you missed to write the line as it was if those keywords were missing, anywas I made it work adding a "else" at at the bottom; thank you really helpful
% Open the file for reading in text mode.
fileID = fopen('./complete_model_test/input.dic', 'rt');
% Open the file for reading in text mode.
outputFileID = fopen('./complete_model_test/test1.dic', 'wt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if contains(textLine, 'Kinetics')
textLineOut = sprintf('%s %f', textLine, 'Cartel1');
elseif contains(textLine, 'Thermodynamics')
textLineOut = sprintf('%s %f', textLine, 'Cartel2');
elseif contains(textLine, 'Output')
textLineOut = sprintf('%s %f', textLine, 'Cartel3');
else
textLineOut = sprintf('%s %f', textLine);
end
% Output line of text to output file
fprintf(outputFileID, '%s\n', textLineOut);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the files.
fclose(fileID);
fclose(outputFileID);
this is the output; I dont know why it adds 67.000
Dictionary kinetics
{
@Kinetics 67.000000artel1
@Thermodynamics 67.000000artel2
@Output 67.000000artel3
}
textLineOut = sprintf('%s %f', textLine);
You are only passing one input to a format that expects two.
textLineOut = sprintf('%s %f', textLine, 'Cartel3');
You are using %f to display the character vector 'Cartel3'. The first character of that is 'C' which happens to be encoded as the integer 67
Andrea Somma
Andrea Somma on 7 May 2022
Edited: Andrea Somma on 7 May 2022
yes I noticed; I found out that removing %f it works fine, thank you!
If the "Cartel" are not numbers in an array, you can do this:
if contains(textLine, 'Kinetics')
textLineOut = sprintf('%s Cartel1', textLine)
elseif contains(textLine, 'Thermodynamics')
textLineOut = sprintf('%s Cartel2', textLine)
elseif contains(textLine, 'Output')
textLineOut = sprintf('%s Cartel3', textLine)
end
Thank you!😉

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!