How to populate listbox with all files in a folder - GUI
1 view (last 30 days)
Show older comments
Hi everyone, I have a problem with trying to populate the listbox in a GUI. I want to click a pushbutton 'browse', select any folder on my computer and have the GUI load all the files in that folder to a listbox (listbox1) so the user can see all the files in that folder. I asked this in an earlier question but cannot comment on the answer so started a new question. So far, thanks to Orion, I have this code under the pushbutton callback function:
% get the folder
folder_name = uigetdir;
% get what is inside the folder
Infolder = dir(folder_name);
% Initialize the cell of string that will be update in the list box
MyListOfFiles = [];
% Loop on every element in the folder and update the list
for i = 1:length(Infolder)
if Infolder(i).isdir==0
MyListOfFiles{end+1,1} = Infolder(i).name;
end
end
% update the listbox with the result
set(handles.listbox1,'String',MyListOfFiles)
When I ran the GUI, I clicked browse, chose a folder and I received this error:
Struct contents reference from a non-struct array object.
Error in GUI_2>pushbutton1_Callback (line 120) set(handles.listbox1,'String',MyListOfFiles)
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in GUI_2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI_2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Any ideas? I feel like I should have some code under the listbox createfcn and listbox callback functions too?
Cheers, Ellis
3 Comments
Stephen23
on 2 Mar 2016
PS: If you had stuck with your original question you would have learned that you can replace most of your code (the loops) with these two lines:
S = dir(folder_name);
N = {S(~[S.isdir]).name}
This is what happens when people ask the same question in multiple locations: they lose track of what info that have been given.
SANKAR JYOTI NATH
on 27 Oct 2016
sir i have used your code and it works perfectly but as a result i found only the files name not the data which contain by the files....i want to open the files in a listbox in GUI...need your help
Answers (1)
Orion
on 2 Mar 2016
Seems you have a problem with the structure handles
The code I showed earlier was made using a gui developped with GUIDE.
I attached the demo gui I made. You can use it to compare with your code and see what's wrong.
1 Comment
david martinez
on 1 Jul 2017
Working diligent
You can help me just to read the file name, not the address
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!