How to populate listbox with all files in a folder - GUI

1 回表示 (過去 30 日間)
Ellis Berry
Ellis Berry 2016 年 3 月 2 日
コメント済み: david martinez 2017 年 7 月 1 日
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
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
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
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 件のコメント
david martinez
david martinez 2017 年 7 月 1 日
Working diligent
You can help me just to read the file name, not the address

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by