load folder's images names to a Matlab listbox

3 visualizzazioni (ultimi 30 giorni)
'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

Risposta accettata

Image Analyst
Image Analyst il 25 Gen 2015
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

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by