MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

Shane
0

[GUI] How to create a directory of files of a specific folder using Listbox

Shane
さんによって質問されました 2012 年 12 月 19 日
最新アクティビティ Image Analyst
さんによって 編集されました 2013 年 10 月 28 日
How to create a directory of files of a specific folder using Listbox.
I basically want to fill a listbox with files from folder.
Thanks, Shane

  0 件のコメント

サインイン to comment.

タグ

1 件の回答

回答者: Image Analyst
2012 年 12 月 19 日
編集済み: Image Analyst
2013 年 10 月 28 日

Use the dir() command. For example see the LoadImageList() function below:
%=====================================================================
% --- Load up the listbox with image files in folder handles.handles.ImageFolder
function handles=LoadImageList(handles)
ListOfImageNames = {};
folder = handles.ImageFolder;
if length(folder) > 0
if exist(folder,'dir') == false
msgboxw(['Folder ' folder ' does not exist.']);
return;
end
% fprintf(1, 'Getting list of images in folder: %s\n', folder);
else
fprintf('No folder specified as input for function LoadImageList.\n');
WarnUser('No folder specified as input for function LoadImageList.');
return;
end
% If it gets to here, the folder is good.
ImageFiles = dir([folder '\*.*']);
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder2, name, extension] = fileparts(baseFileName);
extension = upper(extension);
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
otherwise
end
end
set(handles.lstImageList,'string',ListOfImageNames);
% Need to deselect everything otherwise if new folder has fewer files than the last folder used, the listbox won't show up.
set(handles.lstImageList,'value', []);
return; % from LoadImageList()

  2 件のコメント

Jan
2012 年 12 月 19 日
exist(folder,'dir') replies 7 if folder is existing and 0 otherwise. Comparing the replied value with false works, but might be misleading.
Since exist() returns an integer (or maybe a double that is an integer) I should be comparing it to an integer. Maybe long ago it used to return a boolean - I don't remember - or maybe I just did that because it seemed more intuitive. I should probably change it (though I've got that code in dozens of files). Thanks for the tip.

サインイン to comment.



Translated by