Listbox, how to populate it?
5 views (last 30 days)
Show older comments
Hello, I am trying to use a listbox that would allow me to select an image from a certain file derectory. Here´s the code.
function listbox1_Callback(hObject, eventdata, handles)
directory=dir('*.jpg');
files={directory.name}';
ptr=get(hObject,'Value');
filename=char(files(ptr));
imimport=imread(filename);
figure,imshow(imimport);
% --- Executes during object creation, after setting all properties.
The thing is, it does not display the the file, its empty. Anyone, please, help?
0 Comments
Answers (3)
Image Analyst
on 8 Jun 2013
Edited: Image Analyst
on 28 Oct 2013
No, that is all wrong. First of all, you don't put the code to load up the listbox with filenames in the callback of the listbox, which gets executed when you click on an item in the listbox. The filenames have to be already in there so that the user can have something to click on. First of all, you need to have a function like LoadListBox(), which you call during your OpeningFcn function, or within the callback for your "Specify folder..." push button. Here's some code for that:
%=====================================================================
% --- Load up the listbox with image files in folder 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()
Next, you need to put this code in the callback for the listbox.
% Get image name
Selected = get(handles.lstImageList, 'value');
% If more than one is selected, bail out.
if length(Selected) > 1
baseImageFileName = '';
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow')
drawnow; % Cursor won't change right away unless you do this.
return;
end
% If only one is selected, display it.
ListOfImageNames = get(handles.lstImageList, 'string');
% Get the name of the item in the listbox that they clicked on.
baseImageFileName = cell2mat(ListOfImageNames(Selected));
% Prepend folder.
fullImageFileName = fullfile(handles.ImageFolder, baseImageFileName);
% Display the image.
imgOriginal = imshow(fullImageFileName);
Walter Roberson
on 8 Jun 2013
Put a breakpoint in at the assignment to imimport. Run the program until you get there. When it stops, examine filename and see if it is what you want. If it is, single-step and check imimport to see if it is the right size and class for what you want. Check class() of it and check min() and max() of it. Single step. See if the image shows up. If a space for it appears but the image is blank, try imagesc(imimport)
Or is the difficulty that the file names do not show up in the listbox? If that is the case, then you need to show the code in which you store the file names into the listbox.
Have you had a look at the "41 complete GUI examples" in the File Exchange?
0 Comments
Chandrasekhar
on 8 Jun 2013
this is how a list box can be populated
str{1} = 'hello'; str{2} = 'world'; set(handles.listbox1,'String',str);
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!