MATLAB Answers

how to insert elements in listbox?

조회 수: 187(최근 30일)
how can i insert list of images into listbox?? I have a folder containg images and i want to insert list of images into the listbox

  댓글 수: 0

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

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 27 Jan 2012
Hi, Usama.
Maybe you are seeking for something like this?
In the openingfcn
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
files = dir(fullfile(pwd,'Folder01','*.png'));
for x = 1 : length(files)
handles.images{x} = imread(fullfile(pwd,'Folder01',files(x).name));
end
set(handles.listbox1,'string',{files.name});
guidata(hObject, handles);
In the listbox1 callback
function listbox1_Callback(hObject, eventdata, handles)
handles.output = hObject;
index = get(handles.listbox1,'value');
imshow(handles.images{index});
guidata(hObject, handles);
You can also change the image extention by replacing png with another extention
Eq: files = dir(fullfile(pwd,'Folder01','*.jpg'));
And if it is DICOM files
files = dir(fullfile(pwd,'Folder01','*.dcm'));
for x = 1 : length(files)
handles.images{x} = dicomread(fullfile(pwd,'Folder01',files(x).name));
end
I hope this helps.

  댓글 수: 8

표시 이전 댓글 수: 5
hush puppy
hush puppy 15 May 2015
hi @chandra, i've used the code provided above..but im getting this error: 'Cell contents assignment to a non-cell array object'. Can you tell me what went wrong? All I get is a blank listbox.
Image Analyst
Image Analyst 15 May 2015
Try
for k = 1 : length(files)
listboxItems{k} = files(k).name;
end
set(handles.listbox1, 'String', listboxItems);
hush puppy
hush puppy 15 May 2015
@image analyst, i've tried your code..this code seem to display all my file names now..but the image is not appearing on my handles.images when I click on it..

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

추가 답변(3개)

Chandra Kurniawan
Chandra Kurniawan 27 Jan 2012
In openingfcn
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.cdir = pwd;
set(handles.listbox1,'enable','off');
guidata(hObject, handles);
In pushbutton callback
function pushbutton1_Callback(hObject, eventdata, handles)
handles.output = hObject;
fn = uigetdir(handles.cdir,'Select directory');
if fn ~= 0
handles.cdir = fn;
img = dir(fullfile(handles.cdir,'*.png'));
for x = 1 : length(img)
handles.I{x} = imread(fullfile(handles.cdir,img(x).name));
end
if length(img) ~= 0, set(handles.listbox1,'enable','on');
else, set(handles.listbox1,'enable','off');
end
set(handles.edit1,'string',handles.cdir);
set(handles.listbox1,'string',{img.name});
end
guidata(hObject, handles);
In listbox callback
function listbox1_Callback(hObject, eventdata, handles)
handles.output = hObject;
index = get(handles.listbox1,'value');
imshow(handles.I{index});
guidata(hObject, handles);
%

  댓글 수: 7

표시 이전 댓글 수: 4
Elysi Cochin
Elysi Cochin 24 Oct 2012
sir i wanted one more listbox in my GUI... the same code when written... listbox1's image1 is showing listbox2's image... but listbox2's images are being displayed correctly... can u rectify my error.... same way i wanted to swap the contents of 2 listboxes.... and also i wanted same contents of listbox1 in listbox2.... please do reply me.....
Walter Roberson
Walter Roberson 24 Oct 2012
Please start a new Question for this, and show your code in that new Question.
Elysi Cochin
Elysi Cochin 31 Jan 2013
hi, chandra, one doubt, in addition to the listbox and axes.... i have two buttons also.... one named "DELETE" and the other named "RESTORE".....
in the DELETE button when i select a particular item from listbox that name gets deleted from the listbox and the corresponding image gets deleted from the folder....
on click of the RESTORE button all the delete images should get displayed in a figure file and added back to the folder.....
please can u help me do this option.....
i tried, its working when only one image is deleted..... if i delete more than one item, only the last deleted item is getting restored..... can u help me sort it out.....

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


Image Analyst
Image Analyst 27 Jan 2012
I don't know how to put images into listboxes. You can put the name of the image files into the listboxes (like Titus showed you) but you can't put the images themselves in with MATLAB only. However, you can put an ActiveX control onto your GUI, and there are third party ActiveX controls that can have images in a listbox, such as ( http://www.atalasoft.com/products/dotimage/winforms). Or you can use Microsoft's "ListView" control: http://msdn.microsoft.com/en-us/library/ie/bb398790.aspx. Scroll down the page about half way until you see the listbox with pictures of bicycles in it.

  댓글 수: 0

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


Titus Edelhofer
Titus Edelhofer 26 Jan 2012
Hi Usama,
something like the following:
files = dir(fullfile(theFolder, '*.jpg'));
set(handles.listbox1, 'string', {files.name})
Titus

  댓글 수: 0

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

Community Treasure Hunt

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

Start Hunting!

Translated by