複数のファイル読み込み
69 views (last 30 days)
Show older comments
[IMG_0100_1.tif,IMG_0100_2.tif,IMG_0100_3.tif,IMG_0100_4.tif,IMG_0100_5.tif,IMG_0110_1.tif,IMG_0110_2.tif,IMG_0110_3.tif,IMG_0110_4.tif,IMG_0110_5.tif ............]
といった末尾の数字に規則性があるファイル名を持つファイルがあるとき,末尾が2,3,4であるファイルだけを連続して読み込むにはどうすればよいでしょうか.
このような感じで...
[IMG_0100_2.tif,IMG_0100_3.tif,IMG_0100_4.tif,IMG_0110_2.tif,IMG_0110_3.tif,IMG_0110_4.tif, ............]
0 Comments
Accepted Answer
Akira Agata
on 20 Nov 2019
あるいは、ファイルが大量にあるようでしたら以下の方法ではいかがでしょうか。
% カレントフォルダにある .tifファイル一覧を作成
s = dir('*.tif');
s = struct2table(s);
% 末尾が _2.tif ~ _4.tif のみを抽出
idx = endsWith(s.name,{'_2.tif','_3.tif','_4.tif'});
s = s(idx,:);
for kk = 1:height(s)
filePath = fullfile(s.folder{kk},s.name{kk});
%
% Read the file and do something
%
end
More Answers (1)
Kazuya
on 20 Nov 2019
Edited: Kazuya
on 20 Nov 2019
一案ですが、ファイルのリストを string 型で作って contains を使えば、該当するファイル名だけのリストを作れるので、それを for ループで回せば良いかと思いました。
例:
filelist = ["IMG_0100_1.tif","IMG_0100_2.tif","IMG_0100_3.tif","IMG_0100_4.tif","IMG_0100_5.tif",...
"IMG_0110_1.tif","IMG_0110_2.tif","IMG_0110_3.tif","IMG_0110_4.tif","IMG_0110_5.tif"]';
idx = contains(filelist,["_2.tif","_3.tif","_4.tif"]);
filelist234 = filelist(idx)
末尾に 2,3,4 を含むファイル名だけが filelist234 に残ります。
filelist234 =
6×1 の string 配列
"IMG_0100_2.tif"
"IMG_0100_3.tif"
"IMG_0100_4.tif"
"IMG_0110_2.tif"
"IMG_0110_3.tif"
"IMG_0110_4.tif"
おおもとの filelist は以下で作れると思います。
listing = ls('*.tif');
filelist = string(listing);
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!