フィルターのクリア

Add a large number of comma separated numbers to a txt file

3 ビュー (過去 30 日間)
lou
lou 2018 年 2 月 28 日
コメント済み: lou 2018 年 2 月 28 日
I have a .txt file that has two columns. It looks like this
Age 3
Salary 52661
Average working hours 40
Conditions 1, 26, 65, 89, 20, 2, 2
I would like to add a row
Favorite numbers 2, 8, 66
Birdman has given an answer to this question in another topic.
fid=fopen('data.txt','a+');
fprintf(fid,'\n%s %d, %d, %d\n',"Favorite numbers",2,8,66);
fclose(fid)
Unfortnuately I forgot to add that the amount of doubles (in my case "favorite number"s) can be humongous (hundreds of numbers).
Is there a way to get the %d multiple times in the code without typing it out hundred of times.
I tried
a='%d, ';
rep=repmat(a,1,k) %k being the number of favorite numbers
fprintf([fid,'\n%s ' rep ' \n','Favorite numbers', vector_with_numbers]);
this doesn't work, just prints rep. Any ideas?

採用された回答

Fangjun Jiang
Fangjun Jiang 2018 年 2 月 28 日
編集済み: Fangjun Jiang 2018 年 2 月 28 日
%d can be used repeatedly automatically, run this to see the effect:
fprintf(1,'\n%s ',"favorite numbers");
fprintf(1,',%d',1:20);
fprintf(1,'\n');
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2018 年 2 月 28 日
編集済み: Fangjun Jiang 2018 年 2 月 28 日
See the update. Add spaces in
'\n%s '
as you want.
lou
lou 2018 年 2 月 28 日
Awesome, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by