Create results .txt files with loop

조회 수: 2(최근 30일)
Ivan Mich
Ivan Mich 2020년 5월 17일
댓글: Ivan Mich 2020년 5월 19일
I am making calculations by reading a .txt file. "data1.txt"
data1.txt has this format:
I am reading the first line of data1.txt the number a=.... after that I am making the calculation c=a+b, where b=5.
I would like to create 2 txt files, one with the result of the first calculation (I mean c=a+b=5+5=10), and one .txt file the result of the second calculation (I mean c=a+b=10+5=15).
How can I do that?

채택된 답변

Stijn Haenen
Stijn Haenen 2020년 5월 19일
you can create txt files with:
  댓글 수: 2
Ivan Mich
Ivan Mich 2020년 5월 19일
One more question. If I want to merge them? I mean I want to have after all iteration to unit test1, test2.txt, to one .txt file with all the results. I mean final .txt file to have vertically the result of test1.txt,
in the second line the results of test2.txt etc.
How could I do that?

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

추가 답변(1개)

Johannes Hougaard
Johannes Hougaard 2020년 5월 19일
Yes you should use fprintf - but you need a format input as well to get new lines.
textfile = 'data1.txt';
fid = fopen(textfile);
filecontent = textscan(fid,'%s','delimiter','\n');
a = cellfun(@str2double,filecontent{1});
b = 5;
c = a+b;

Community Treasure Hunt

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

Start Hunting!

Translated by