Sum of numbers from a text file

9 ビュー (過去 30 日間)
pink flower
pink flower 2020 年 4 月 1 日
コメント済み: Image Analyst 2020 年 4 月 1 日
I have a text file with 288 rows. I want to add the values of 6 in 6 lines. For example:
1,00
1,00
1,00
0,89
1,00
1,00
1,00
1,00
1,00
0,22
0,03
0,00
0,86
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
1,00
0,44
0,08
0,14
0,00
0,00
0,44
1,00
0,03
0,97
1,00
...
I want to add 1,00 1,00 0,89 1,00 1,00 1,00 and put the result in a row. Then add the next 6 numbers and place in the same file as above, but now on the line below. How can I do this?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 1 日
Try this. Here test.txt is the name of your file. This code will create a new file and save the sums in it.
data = fileread('test.txt');
data = strrep(data, ',', '.');
num_data = sscanf(data, '%f\n');
sum_6 = sum(reshape(num_data, 6, []), 1)';
f = fopen('new_file.txt', 'w');
fprintf(f, '%1.2f\n', sum_6);
fclose(f);
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 1 日
Glad to be of help.
Image Analyst
Image Analyst 2020 年 4 月 1 日
Amanda, can you go ahead and accept this answer?
By the way, there are lots of ways to read in numbers, such as importdata(), dlmread(), csvread(), readmatrix(), load(), etc.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by