0% found this document useful (0 votes)
154 views

How To Insert Elements in Listbox - MATLAB Answers - MATLAB Central

The document discusses how to insert a list of image files into a listbox in MATLAB. It provides code that: 1) Gets a list of image files from a folder and stores them in a cell array. 2) Populates the listbox with the file names. 3) Loads the corresponding image when an item is selected and displays it.

Uploaded by

Akanksha221291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

How To Insert Elements in Listbox - MATLAB Answers - MATLAB Central

The document discusses how to insert a list of image files into a listbox in MATLAB. It provides code that: 1) Gets a list of image files from a folder and stores them in a cell array. 2) Populates the listbox with the file names. 3) Loads the corresponding image when an item is selected and displays it.

Uploaded by

Akanksha221291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Vote

howtoinsertelementsinlistbox?
AskedbyUsamaJavedon26Jan2012
LatestactivityCommentedonbyhushpuppyon15May2015
AcceptedAnswerbyChandraKurniawan
568views(last30days)

howcaniinsertlistofimagesintolistbox??Ihaveafoldercontaingimagesandiwanttoinsertlistofimages
intothelistbox

0Comments

Tags
gui guide imageprocessing image

Products
Noproductsareassociatedwiththisquestion.

RelatedContent

4Answers
Vote
4
Link
AnswerbyChandraKurniawanon27Jan2012
Acceptedanswer

Hi,Usama.

Maybeyouareseekingforsomethinglikethis?

Intheopeningfcn

functionuntitled_OpeningFcn(hObject,eventdata,handles,varargin)
handles.output=hObject;
files=dir(fullfile(pwd,'Folder01','*.png'));
forx=1:length(files)
handles.images{x}=imread(fullfile(pwd,'Folder01',files(x).name));
end
set(handles.listbox1,'string',{files.name});
guidata(hObject,handles);

Inthelistbox1callback

functionlistbox1_Callback(hObject,eventdata,handles)
handles.output=hObject;
index=get(handles.listbox1,'value');
imshow(handles.images{index});
guidata(hObject,handles);
Youcanalsochangetheimageextentionbyreplacingpngwithanotherextention

Eq:files=dir(fullfile(pwd,'Folder01','*.jpg'))

AndifitisDICOMfiles

files=dir(fullfile(pwd,'Folder01','*.dcm'));
forx=1:length(files)
handles.images{x}=dicomread(fullfile(pwd,'Folder01',files(x).name));
end

Ihopethishelps.

8Comments

Show5oldercomments

hushpuppyon15May2015

hi@chandra,i'veusedthecodeprovidedabove..butimgettingthiserror:'Cellcontentsassignment
toanoncellarrayobject'.Canyoutellmewhatwentwrong?AllIgetisablanklistbox.


ImageAnalyston15May2015

Try

fork=1:length(files)
listboxItems{k}=files(k).name;
end
set(handles.listbox1,'String',listboxItems);


hushpuppyon15May2015

@imageanalyst,i'vetriedyourcode..thiscodeseemtodisplayallmyfilenamesnow..buttheimage
isnotappearingonmyhandles.imageswhenIclickonit..
Vote
4
Link
AnswerbyChandraKurniawanon27Jan2012

Inopeningfcn

functionuntitled_OpeningFcn(hObject,eventdata,handles,varargin)
handles.output=hObject;
handles.cdir=pwd;
set(handles.listbox1,'enable','off');
guidata(hObject,handles);

Inpushbuttoncallback

functionpushbutton1_Callback(hObject,eventdata,handles)
handles.output=hObject;
fn=uigetdir(handles.cdir,'Selectdirectory');
iffn~=0
handles.cdir=fn;
img=dir(fullfile(handles.cdir,'*.png'));
forx=1:length(img)
handles.I{x}=imread(fullfile(handles.cdir,img(x).name));
end
iflength(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);

Inlistboxcallback

functionlistbox1_Callback(hObject,eventdata,handles)
handles.output=hObject;
index=get(handles.listbox1,'value');
imshow(handles.I{index});
guidata(hObject,handles);
MATLABAnswers

7Comments

Show4oldercomments

ElysiCochinon24Oct2012

siriwantedonemorelistboxinmyGUI...thesamecodewhenwritten...listbox1'simage1isshowing
listbox2'simage...butlistbox2'simagesarebeingdisplayedcorrectly...canurectifymyerror....same
wayiwantedtoswapthecontentsof2listboxes....andalsoiwantedsamecontentsoflistbox1in
listbox2....pleasedoreplyme.....


WalterRobersonon24Oct2012

PleasestartanewQuestionforthis,andshowyourcodeinthatnewQuestion.


ElysiCochinon31Jan2013

hi,chandra,onedoubt,inadditiontothelistboxandaxes....ihavetwobuttonsalso....onenamed
"DELETE"andtheothernamed"RESTORE".....

intheDELETEbuttonwheniselectaparticularitemfromlistboxthatnamegetsdeletedfromthe
listboxandthecorrespondingimagegetsdeletedfromthefolder....

onclickoftheRESTOREbuttonallthedeleteimagesshouldgetdisplayedinafigurefileandadded
backtothefolder.....
pleasecanuhelpmedothisoption.....

itried,itsworkingwhenonlyoneimageisdeleted.....ifideletemorethanoneitem,onlythelast
deleteditemisgettingrestored.....canuhelpmesortitout.....


MATLABAnswers
Vote
1
Link
AnswerbyImageAnalyston27Jan2012

Idon'tknowhowtoputimagesintolistboxes.Youcanputthenameoftheimagefilesintothelistboxes(like
Titusshowedyou)butyoucan'tputtheimagesthemselvesinwithMATLABonly.However,youcanputan
ActiveXcontrolontoyourGUI,andtherearethirdpartyActiveXcontrolsthatcanhaveimagesinalistbox,such
as(http://www.atalasoft.com/products/dotimage/winforms).OryoucanuseMicrosoft's"ListView"control:
http://msdn.microsoft.com/enus/library/ie/bb398790.aspx.Scrolldownthepageabouthalfwayuntilyouseethe
listboxwithpicturesofbicyclesinit.

0Comments

Vote
0
Link
AnswerbyTitusEdelhoferon26Jan2012
HiUsama,

somethinglikethefollowing:

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

Titus

0Comments

MATLABAnswers

MATLABandSimulinkresourcesforArduino,LEGO,andRaspberryPi

Learnmore

DiscoverwhatMATLABcandoforyourcareer.
Opportunitiesforrecentengineeringgrads.

ApplyToday

MATLABAcademy
NewtoMATLAB?

LearnMATLABtoday!
19942016TheMathWorks,Inc.

MATLABAnswers

You might also like