필터 지우기
필터 지우기

Listbox, how to populate it?

조회 수: 14 (최근 30일)
Maria Lopez
Maria Lopez 2013년 6월 8일
댓글: Image Analyst 2013년 10월 29일
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?

답변 (3개)

Image Analyst
Image Analyst 2013년 6월 8일
편집: Image Analyst 2013년 10월 28일
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);
  댓글 수: 1
Image Analyst
Image Analyst 2013년 10월 29일
Maria, did my fix work for you???

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 6월 8일
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?

Chandrasekhar
Chandrasekhar 2013년 6월 8일
this is how a list box can be populated
str{1} = 'hello'; str{2} = 'world'; set(handles.listbox1,'String',str);

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by