How can I change fileID name in a for loop?

1 回表示 (過去 30 日間)
Maazen ALSABAAN
Maazen ALSABAAN 2012 年 11 月 14 日
Hi, I have this for loop
for i=1:3
if i==1
fid = fopen('report_1.xls', 'wt');
end
if i==2
fid = fopen('report_2.xls', 'wt');
end
if i==3
fid = fopen('report_3.xls', 'wt');
end
end
In fact, I have much bigger than this loop. The problem is that I have to repeat fopen for each i. My question is can I avoid the if statements. So, the value of i can be written in fileID without adding it manually. For example,
for i=1:3
fid = fopen('report_i.xls', 'wt');
end
The above example will create a file named "report_i". However, I want to create 3 files named "report_1" , "report_2", and "report_3". How can I do that?
Thank you so much in advance

採用された回答

Pedro Villena
Pedro Villena 2012 年 11 月 14 日
編集済み: Pedro Villena 2012 年 11 月 14 日
for i=1:3
fid(i) = fopen(sprintf('report_%d.xls',i), 'wt');
end
  1 件のコメント
Maazen ALSABAAN
Maazen ALSABAAN 2012 年 11 月 14 日
Thank you! I appreciate it

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

その他の回答 (1 件)

C.J. Harris
C.J. Harris 2012 年 11 月 14 日
  2 件のコメント
Maazen ALSABAAN
Maazen ALSABAAN 2012 年 11 月 14 日
Thank you! I appreciate it
Jan
Jan 2012 年 11 月 14 日
And when you read this section of the FAQ, read the rest also. It is very efficient to profit from the mistakes, which have been made repeatedly by others.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by