write in a txt file

조회 수: 4 (최근 30일)
Andrea Somma
Andrea Somma 2022년 5월 7일
댓글: Andrea Somma 2022년 5월 8일
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

채택된 답변

Image Analyst
Image Analyst 2022년 5월 7일
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
Image Analyst
Image Analyst 2022년 5월 7일
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
Andrea Somma
Andrea Somma 2022년 5월 8일
Thank you!😉

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Thermodynamics and Heat Transfer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by