for文を使って連番のテキストファイルに書き込む

조회 수: 19 (최근 30일)
H.K
H.K 2023년 3월 2일
편집: H.K 2023년 3월 2일
for文を使って以下のexp.txtを"1.txt、2.txt、3.txt"のように連番で書き込みが
できるようfopenしたいと考えています。
fileID = fopen('exp.txt','w');
何か良い方法はないでしょうか。
具体的には下記のFileIDを連番にしてfor以下の内容を書き込みたいです。
fileID = fopen('exp.txt','w');
for j = 3:7:38
F = x6(:,j);
F1 = max(F)
fprintf(fileID,'%6.2f\n',F1);
end
fclose(fileID);
ご助言の程宜しくお願い致します。

채택된 답변

Hernia Baby
Hernia Baby 2023년 3월 2일
편집: Hernia Baby 2023년 3월 2일
もしかするとfileIDをファイル名と混同されてるかもしれないので2通り書きます。
①exp.txt に x6 の指定列の最大を書く方法
clc,clear;
x6 = randi([0 100],38);
fileID = fopen('exp.txt','w');
for jj = 3:7:38
fprintf(fileID,'%6.2f\n',max(x6(:,jj)));
end
fclose(fileID);
②テキスト名を変更して保存する方法
clc,clear;
idx = 3:7:38;
x6 = randi([0 100],38);
for ii = 1:length(idx)
jj = idx(ii);
F1 = max(x6(:,jj));
F2 = sprintf('%6.2f',F1);
writematrix(F2,sprintf('%i.txt',ii))
end
  댓글 수: 1
H.K
H.K 2023년 3월 2일
②番目の方法で実行した結果うまくいきました。
ご助言頂き有難う御座います。

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB 入門에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!