How to populate Listbox in GUI with all files from a folder.

Hi Everybody, What I want is quite simple but I'm struggling tremendously trying to do it. I have a GUI with a 'Browse' button that I want the user to be able to click, choose a folder and then all the files in that folder are listed in the listbox. I seem to be able to get as far as browsing files by simply using the uigetdir syntax but when I select a folder it wont load to the listbox. Any ideas?
Cheers, Ellis

回答 (1 件)

Orion
Orion 2016 年 3 月 2 日

1 投票

Hi Ellis,
let say you have one pushbutton and one listbox (tagged pushbutton1 and listbox1 with GUIDE).
in the callback of your pushbutton, you should do something like :
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
That should do it

4 件のコメント

Jan
Jan 2016 年 3 月 2 日
編集済み: Jan 2016 年 3 月 2 日
Or without a loop:
Infolder = dir(folder_name);
MyListOfFiles = {Infolder(~[Infolder.isdir]).name}
Orion
Orion 2016 年 3 月 2 日
A little more elegant indeed
Jan
Jan 2016 年 3 月 2 日
Less chances for creating a bug by a typo :-) And no warnings for a missing pre-allocation.
mechE
mechE 2019 年 5 月 13 日
How to do the same thing in a colum table in ui. I want to populate the colum of a table in the same way but I am getting some java string error.

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

カテゴリ

ヘルプ センター および File Exchange で Graphics Performance についてさらに検索

質問済み:

2016 年 3 月 2 日

コメント済み:

2019 年 5 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by