for文を使ったテキストファイルの更新
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
H.K
am 2 Mär. 2023
Bearbeitet: Atsushi Ueno
am 2 Mär. 2023
初心者です。開いたファイルにfor文を使って数字を追加しようと思うのですが、
やり方が自力でできません。
for 1:10;
F1 = max(x1(:,jj));
fileID = fopen('1.txt','w');
fprintf(fileID,'%5d\n',F1)
end
これで実行しているのですが、最後の10しか1.txtに入力されません。
1.txtに1,2,3,4・・・10まで書けるようにしたいのですが、どうすればよろしいでしょうか。
ご助言お願い致します。
0 Kommentare
Akzeptierte Antwort
Hernia Baby
am 2 Mär. 2023
fileIDがforループ内に入ってるからだと思います
まずは1.txtを適当に作ります
clear,clc;
a = [];
writematrix(a,'1.txt');
dir('*.txt')
fileIDを外に出してください
x1 = randi([1 10],10);
fileID = fopen('1.txt','w');
for jj = 1:10
F1 = max(x1(:,jj));
fprintf(fileID,'%5d\n',F1);
end
fclose(fileID);
中身を確認します
type('1.txt')
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu ファイルの操作 finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!