load folder's images names to a Matlab listbox
4 次查看(过去 30 天)
显示 更早的评论
'm trying to load all the images' names that exist in a particular folder that i selected using matlab GUI into a listbox.
the problem is that when i select the folder:
if it's empty, I can see the list empty with a white background color (which is the right thing).
But when i select a folder that contains images, the listbox disappears from the GUI. and i get a warning saying:
Warning: single-selection listbox control requires a scalar Value
Control will not be rendered until all of its parameter values are valid
i'm stuck in this issue for a long time, and i couldn't find a way to solve it.
here's the code i tried:
% --- Load up the listbox with tif files in folder handles.handles.ImageFolder
function handles=LoadImageList(handles)
ListOfImageNames = {};
folder = handles.ImageFolder;
if ~isempty(handles.ImageFolder)
if exist(folder,'dir') == false
warningMessage = sprintf('Note: the folder used when this program was last run:\n%s\ndoes not exist on this computer.\nPlease run Step 1 to select an image folder.', handles.ImageFolder);
msgboxw(warningMessage);
return;
end
else
msgboxw('No folder specified as input for function LoadImageList.');
return;
end
% If it gets to here, the folder is good.
ImageFiles = dir([handles.ImageFolder '/*.*']);
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder, name, extension] = fileparts(baseFileName);
extension = upper(extension);
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif', '.avi'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
otherwise
end
end
set(handles.lstImageList,'string',ListOfImageNames);
return
0 个评论
采纳的回答
Image Analyst
2015-1-25
You will get that error message if you had one folder and had, say, the 10'th item in the listbox selected. Then if you went to another folder and called LoadImageList() and that folder only had, say, 5 images. The listbox would still have a "value" property of 10 but there are only 5 items there. It can't select the 10th item, so it throws that warning message.
What you need to do is to add something like this after the for loop
if length(ListOfImageNames) >= 1
set(handles.lstImageList, 'value', 1); % Select first item in listbox.
else
set(handles.lstImageList, 'value', []); % Deselect all items.
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!