How to return files with a specific extension using 'dir'?

790 ビュー (過去 30 日間)
John
John 2014 年 8 月 11 日
コメント済み: Stephen23 2021 年 8 月 17 日
The command
dir *.bmp
will print to the command window the names of all files in the current directory ending with the extension .bmp. I know the following is not valid MATLAB syntax, but is there a command that would be the equivalent of
fileList = dir *.bmp;
I want to create a list of all files ending with a specific extension using minimal code. Currently I have the following:
files = dir;
L = length(files);
index = false(1, L);
for k = 1:L
M = length(files(k).name);
if M > 4 && strcmp(files(k).name(M-3:M), '.bmp')
index(k) = true;
end
end
images = files(index);
There must be a simpler way. Thank you in advance for any comments.
  1 件のコメント
Stephen23
Stephen23 2021 年 8 月 17 日
"There must be a simpler way."
Of course, just use function sytnax rather than command syntax:

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

採用された回答

Image Analyst
Image Analyst 2014 年 8 月 11 日
Try
fileList = dir('*.bmp');
Also, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F, particulalry the second option.
  3 件のコメント
Sebastian Amann
Sebastian Amann 2020 年 4 月 29 日
NOTE: will also return folders with name myfolder.bmp
Image Analyst
Image Analyst 2020 年 4 月 29 日
True. But I don't think it's wise to have a folder with the name that a single image typically has. Virtually always, files the end in .bmp are image files, not folders.

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

その他の回答 (1 件)

Tron
Tron 2018 年 4 月 27 日
Old question, but I found another useful way of doing this. If you have a specific directory you want to search in, you can use a combination of dir and fullfile.
Try
folder = uigetdir();
fileList = dir(fullfile(folder, '*.bmp'));
  3 件のコメント
Sebastian Priebe
Sebastian Priebe 2021 年 5 月 20 日
Matlab is neither freeware, nor does this set it apart from them. For example in python you can just use the tkinter module and get the same.
Sean Nomoto
Sean Nomoto 2021 年 8 月 17 日
or pyqt5 too

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

カテゴリ

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