How to print in a text file?

41 ビュー (過去 30 日間)
Ashesh Choudhury
Ashesh Choudhury 2020 年 10 月 20 日
コメント済み: Ameer Hamza 2020 年 10 月 29 日
I have to matrices x=[1;2;3] and U=[1 2 3; 4 5 6; 7 8 9].
I want to generate a .txt file of the name "output.txt" which would contain the following:
"
This is the output
x
1
2
3
U
1 2 3
4 5 6
7 8 9
"
How to do it?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 20 日
A slightly unusual way but it works
x=[1;2;3];
U=[1 2 3; 4 5 6; 7 8 9];
f = fopen('data.txt', 'w');
fprintf(f, 'This is the output\n');
fprintf(f, 'x\n');
writematrix(x, 'data.txt', 'WriteMode', 'append');
fseek(f, 0, 1);
fprintf(f, 'U\n');
writematrix(U, 'data.txt', 'WriteMode', 'append');
fclose(f);
  7 件のコメント
Ashesh Choudhury
Ashesh Choudhury 2020 年 10 月 27 日
Is there any way to control the number of digits after decimal that is being printed by writematrix?
Ameer Hamza
Ameer Hamza 2020 年 10 月 29 日
Not possible using writematrix, but you can use fprintf()
x=[1;2;3];
U=[1 2 3; 4 5 6; 7 8 9];
f = fopen('data.txt', 'w');
fprintf(f, 'This is the output\n');
fprintf(f, 'x\n');
fprintf(f, [repmat('%.2f', 1, size(x,2)) '\n'], x);
fprintf(f, 'U\n');
fprintf(f, [repmat('%.2f,', 1, size(U,2)) '\n'], U);
fclose(f);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by