Borrar filtros
Borrar filtros

for文を使ったテキストファイルの更新

3 visualizaciones (últimos 30 días)
H.K
H.K el 2 de Mzo. de 2023
Editada: Atsushi Ueno el 2 de Mzo. de 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まで書けるようにしたいのですが、どうすればよろしいでしょうか。
ご助言お願い致します。

Respuesta aceptada

Hernia Baby
Hernia Baby el 2 de Mzo. de 2023
fileIDがforループ内に入ってるからだと思います
まずは1.txtを適当に作ります
clear,clc;
a = [];
writematrix(a,'1.txt');
dir('*.txt')
1.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')
10 10 9 9 10 10 10 10 10 10
  2 comentarios
H.K
H.K el 2 de Mzo. de 2023
初歩中の初歩でした。。。
何度もすみません。おかげさまで思っていた通り実行させることができました。
有難う御座いました。
Hernia Baby
Hernia Baby el 2 de Mzo. de 2023
@H.Kさんが質問してくれたおかげで救われる誰かもいますので、どんどん質問してください。

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!