Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to insert elements in listbox?

Asked by Usama Javed on 26 Jan 2012

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 Comments

Usama Javed

Products

No products are associated with this question.

5 Answers

Answer by Chandra Kurniawan on 27 Jan 2012
Accepted answer

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.

5 Comments

Usama Javed on 27 Jan 2012

i want to loaddir thorugh browse button...
i mean there is a browse button when i click that button than those images should be loaded into the listbox...
can u tell me that plz...

Chandra Kurniawan on 27 Jan 2012

Hi, Usama :)
Just little hint : uigetdir
http://i1196.photobucket.com/albums/aa410/nasa078/Mathworks/uigetdir01.jpg

Usama Javed on 27 Jan 2012

i have used it sir,but its not showing in the listbox

handles.output=hObject;
dirr=uigetdir('C:\Users\Asad\Documents\MATLAB\','Select a Case')
files = dir(fullfile(pwd,dirr,'*.dcm'));
for x=1 : length(files)
handles.images{x}= dicomread(fullfile(pwd,'Case1',files(x).name));
end
set(handles.listbox1,'string',{files.name});
guidata(hObject, handles);

Chandra Kurniawan
Answer by Chandra Kurniawan on 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 Comments

Elysi Cochin on 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 on 24 Oct 2012

Please start a new Question for this, and show your code in that new Question.

Elysi Cochin on 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.....

Chandra Kurniawan
Answer by Image Analyst on 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 Comments

Image Analyst
Answer by Titus Edelhofer on 26 Jan 2012

Hi Usama,

something like the following:

files = dir(fullfile(theFolder, '*.jpg'));
set(handles.listbox1, 'string', {files.name})

Titus

0 Comments

Titus Edelhofer
Answer by Elysi Cochin on 24 Oct 2012

how to copy the contents of one listbox to another listbox....

3 Comments

Walter Roberson on 24 Oct 2012
set(handles.listbox1, 'String', get(handles.listbox2, 'String'));
Elysi Cochin on 24 Oct 2012

thank u so much...

Muhammad on 12 Sep 2013

Good too see this demo and this helped me a lot. but i have a question here.

  • * * i programmed a pushbutton which do zoom
  • * * i have another pushbutton which is contoured and contoure the some part of image by imfreehand and then make mask and calculate the all coodinates in that region. i need another pushbutton (creat excel file) that will show those contoures results on excel sheet.

Zoom button

 if true
 zoom on; % use mouse button to zoom in or out
waitfor(gcf,'CurrentCharacter',13)
zoom reset
zoom off
set(handles.listbox1,'string',{img.name}); 
  end

Contour Button

if true

    function Contourpushbutton_Callback(hObject, eventdata, handles)
% imfreehand
% the figure, wait for the most recent key to become the return/enter key
h=imfreehand(gca,'closed',0);
pos = getPosition(h);
  end

if true

 h=imfreehand(gca,'closed',0);
pos = getPosition(h);
sz = size(handles.listbox1);
maskedImage = poly2mask(pos(:,1), pos(:,2), sz(1), sz(2));
imshow(maskedImage)
[rows, columns] = find(maskedImage ~= 0);
data=[rows,columns];  

end

Elysi Cochin

Contact us