How to populate listbox with all files in a folder - GUI
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2016-3-2
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
2016-10-27
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
回答(1 个)
Orion
2016-3-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!