How to return files with a specific extension using 'dir'?
이전 댓글 표시
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
2021년 8월 17일
"There must be a simpler way."
Of course, just use function sytnax rather than command syntax:
채택된 답변
추가 답변 (1개)
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
Abhilash Sukumari
2019년 10월 9일
Excellent answer. Such an approach with UI implementation is what sets MATLAB apart from other freeware platforms. It's all inbuilt with one package.
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
2021년 8월 17일
or pyqt5 too
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!