How do I print the filenames with for and assign them to another variable?

1 回表示 (過去 30 日間)
studentmatlaber
studentmatlaber 2021 年 4 月 22 日
コメント済み: Rena Berman 2021 年 5 月 6 日
I have 10 .mat files. "data1.mat", "data2.mat", "data2.mat" ... I want to store these files in an array named "file" by throwing them into a for loop. I wrote a code like this for him.
for i = 1:10
file = sprintf ('data% d.mat', i);
if exist (file) == 2
end
end
however, it writes the file "data10.mat" to the file. I want files starting from 1 to 10 to be uploaded to the "file" file. How can I do it?

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 4 月 22 日
for i = 1:10
file = sprintf ('data%d.mat', i)
if exist (file) == 2
fprintf('file "%s" exists with type 2\n', file);
else
fprintf('file "%s" does not exist with type 2\n', file)
end
end
file = 'data1.mat'
file "data1.mat" exists with type 2
file = 'data2.mat'
file "data2.mat" does not exist with type 2
file = 'data3.mat'
file "data3.mat" does not exist with type 2
file = 'data4.mat'
file "data4.mat" does not exist with type 2
file = 'data5.mat'
file "data5.mat" does not exist with type 2
file = 'data6.mat'
file "data6.mat" does not exist with type 2
file = 'data7.mat'
file "data7.mat" does not exist with type 2
file = 'data8.mat'
file "data8.mat" does not exist with type 2
file = 'data9.mat'
file "data9.mat" does not exist with type 2
file = 'data10.mat'
file "data10.mat" does not exist with type 2
(It happens that data1.mat exists in one of the Sym Biology examples.)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by