Filter a directory and add in listbox only files with TIF/TIFF and containing a certain String

10 ビュー (過去 30 日間)
Hello, I am trying to list all tif images ina folder into a listbox. I have it working but am trying to now only list those tif files that also contain a string in their name:
I initially choose the first file (using uigetfile so have the extension, path and filename)
%List all tif images into the list box.
[pathstr,name,ext] = fileparts(openpath);
filePattern = fullfile(pathstr, ['*',ext])
myFiles = dir(filePattern);
ListOfImageNames = {}; % Initialize
for Index = 1:length(myFiles)
% Get the base filename and extension.
baseFileName = myFiles(Index).name;
[folder, name, extension] = fileparts(baseFileName);
% Examine extensions for ones we want.
extension = upper(extension);
switch lower(extension)
case {'.tif','.tiff'}
% Keep only JPG, TIF, or tiff image files.
ListOfImageNames = [ListOfImageNames baseFileName];
% otherwise
end
end
% Now we have a list of validated filenames that we want.
% Send the list of validated filenames to the listbox. %ListOfImageNames;
app.ListBox.Items=ListOfImageNames;
So I thought if I have a string 'NameFilter', then all I need to do is change
filePattern = fullfile(pathstr, ['*',ext])
To
filePattern = fullfile(pathstr, [Namefilter,'*',ext])
But its not quite working. so to summarise, I only want tiff files which contain the string NameFilter
Thanks
Jason

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 7 日
pathstr = fileparts(openpath);
myFiles = dir(pathstr);
filenames = {myFiles.name};
mask = endsWith(filenames, {'.tif', '.tiff'}, 'IgnoreCase', true);
ListOfImages = filenames(mask);
  2 件のコメント
Jason
Jason 2021 年 2 月 7 日
編集済み: Jason 2021 年 2 月 7 日
So simple! Thankyou
Ajh, the only thing is that the string I want the files to contain (Namefilter) is always at the front of the string
Walter Roberson
Walter Roberson 2021 年 2 月 7 日
mask = mask & beginsWith(filenames, Namefilter);
With or without the 'IgnoreCase' option as appropriate.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by