Create results .txt files with loop
2 ビュー (過去 30 日間)
古いコメントを表示
I am making calculations by reading a .txt file. "data1.txt"
data1.txt has this format:
5
10
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?
0 件のコメント
採用された回答
Stijn Haenen
2020 年 5 月 19 日
you can create txt files with:
FP=fopen(sprintf('test%g.txt',1),'wt');
fprintf(FP,num2str(data));
fclose(FP);
その他の回答 (1 件)
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');
fclose(fid);
a = cellfun(@str2double,filecontent{1});
b = 5;
c = a+b;
fid=fopen('data2.txt','wt+');
fprintf(fid,'%d\n',c);
fclose(fid);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!